I want to save the activity state when it is terminated abnormally using the current onSaveInstanceState.
public static Google_map_fragment obj_google_map_fragment;
public static Control_fragment obj_Conntrol_fragment;
public Context Main_context = Main_Activity.this;
FragmentTransaction frt;
TedPermission myTed = null;
private BackPressCloseHandler backPressCloseHandler;
There are currently two fragment objects and the remaining objects in Main_activity.
When I tried to save it through onSaveInstanceState, it was only .putString, .putInt. Is there a put that saves the object itself? Or how can I save it?
Also, there are currently two fragments, so do I need to save and restore each of them?
application android onsaveinstancestate
For objects, you can implement Serializable
or Parcelable
and save it to Bundle
. Note that Bundle has a limitation on the size of data that can be stored, so it is recommended that you store relatively light data, and it is recommended that you establish a storage/recovery strategy by mixing View Model
, Database
, and Preference
.
Activity
and Fragment
have data stored/restored by default. Saving/restoring layout layers is one of them. Therefore, if you use onSaveInstanceState()
onRestoreInstanceState()
and
onRestoreInstanceState()
, you must invoke the super method, and if Activity
preserve the fragment
stateAlternatively, Fragment
saves and restores Bundle
data previously stored as setArguments()
by default. In addition, it would be helpful to look at the internal code of Activity and Fragment to see what data is stored and restored by default.
onSaveInstanceState()
can be used to store the widget's state value or the variable value to be preserved, for example, if you have EditText, or to store the light object itself.
When you enable the Do not archive activities
or Do not keep activities
function in the Settings - Developer Options
, the onSaveInstanceState() / onRestoreInstanceState() situation is easy to test because the activity instance is always shut down when the Home button is clicked while the app is running.
I think you can use this test method to set up a strategy to actually store/recover what data.
© 2024 OneMinuteCode. All rights reserved.