onSaveInstanceState is not getting called

后端 未结 3 2012
南旧
南旧 2021-01-14 00:40

I have an activity which starts various activities for result codes and on getting results in onActivityResult method it starts appropriate activity based on re

相关标签:
3条回答
  • 2021-01-14 01:05

    I had the same issue happening to me on a "drawer menu" part of a "main activity", because I had overridden the "onSaveInstanceState" method in my main activity but had forgotten the call to super.onSaveInstanceState() (which, as a result, would never call the "onSaveInstanceState()" method of my "drawer menu" which was part of this main activity).

    In other words: make sure you don't forget your calls to "super.onSaveInstanceState()" where necessary.

    0 讨论(0)
  • 2021-01-14 01:08

    The method onSaveInstanceState() isn't called when an Activity finishes naturally like on a back button press. That's your app itself destroying the Activity. The method is only called if the Android OS anticipates that it may have to kill your activity to reclaim resources.

    If the Activity then actually gets killed by Android, the OS will make sure you receive a call to onRestoreInstanceState() as well passing the same bundle you used to save your activity's state in onSaveInstanceState() method.

    From the docs:

    This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

    0 讨论(0)
  • 2021-01-14 01:14

    onSaveInstanceState() is only called if the Activity is being killed.

    I don't know what exactly you want to do in that method, but you probably should move your code to the corresponding methods of the Activity Lifecycle.

    from http://developer.android.com/reference/android/app/Activity.html :

    Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

    Also the method description for onSaveInstanceState() describes exactly your situation:

    Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

    0 讨论(0)
提交回复
热议问题