Android SaveInstanceState - Understanding

谁说胖子不能爱 提交于 2019-12-19 05:08:54

问题


From this page of Android SDK

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)).

So is there a mechanism that automatically saves the Activity state without saving value from each element in the activity? I am confused about the above statement.

For an example, Activity A invoked Activity B. In Activity B, I have checboxes, Radio Buttons, etc. User select their choices and click Back button. I am showing the Activity At this point, I want to save the user selection. When user again comes back from Activity A to B, by clicking a button, I would like to see all selections persisted. One way I can think of is, setting the Intent Flag to bring the Activity to fore. But not a recommended method, I think.

So is there a default implementation to save the state, per the above text from SDK? Or may be I am interpreting it wrong?


回答1:


onSaveInstanceState() and onRestoreInstanceState() are only explicitly called by Android when the Activity needs to be recreated, generally after a configuration change (ex. changing orientation). This doesn't cover the case when you have invoked a new instance of the Activity. When you press the back button, Activity B is destroyed, and you are creating a new instance of it the next time you start that Activity.

If you want to manually save the instance of an Activity, invoke Activity B via startActivityForResult(). Then, in Activity B, override the onDestroy() method, and call these lines of code:

@Override
protected void onDestroy() {

    Bundle savedState = new Bundle();
    onSaveInstanceState(savedState);
    Intent data = new Intent();
    data.putExtra("savedState", savedState);
    setResult(RESULT_OK, data);

    super.onDestroy();
}

In Activity A, override on onActivityResult and save the data:

Bundle activityBData;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK) {
        activityBData = data.getBundleExtra("saved_state");
    }
}

Then, when starting Activity B again, call it like so:

Intent intent = new Intent(this, ActivityB.class);
if (activityBData != null) {
    intent.putExtra("saved_state", activityBData);
}
startActivityForResult(intent, 0);

And lastly, in Activity B's onCreate method, restore the state:

if (savedInstanceState == null) {
    Intent intent = getIntent();
    Bundle savedState = intent.getBundleExtra("saved_state");
    onRestoreInstanceState(savedState);
}



回答2:


As per the documentation it will bring your activity forward when you start again ,if it is not killed .but to get all other views state back you need to store them in bundle in onSaveInstanceState() and set it again in onRestoreInstanceState().The default implementation works only for your activity not for your subviews in your activity




回答3:


when an app loses focus to another app onSaveInstanceState() is called but when you navigate back to your app onRestoreInstanceState() may not be called. i.e. if your activity was NOT killed during the period when other activity was in front onRestoreInstanceState() will NOT be called because your activity is pretty much "alive".

All in all, as stated in the documentation for onRestoreInstanceState():

Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

For ex: From B you call startActivity(A). Then from A you call finish() to get back to B. In that case Your first activity, B will not have been destroyed, and neither onCreate() nor onRestoreInstanceState() will be called. These methods are only called when needed, that is when an activity has been destroyed and needs to be recreated by the system.




回答4:


Use the SharedPreferences mechanism. Check out the documentation: https://developer.android.com/reference/android/content/SharedPreferences An example of implementation: https://www.tutorialspoint.com/android/android_shared_preferences.htm Also you can make use of PreferenceFragment to make this task easy.



来源:https://stackoverflow.com/questions/10492637/android-saveinstancestate-understanding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!