Is the onSaveInstanceState (Bundle) method called after onPause ()?

I am new to android, and I read a book for newbies that said that onSaveInstanceState(Bundle) would be called before the system returns your Activity . I tried this on some test codes and found it wrong. I found that onSaveInstanceState(Bundle) is called every time after onPause() called. And this has nothing to do with system recovery. I'm not very sure about this, so the question is: when is onSaveInstanceState(Bundle) actually called?

+6
source share
2 answers

According to Android documentation :

In addition, the onSaveInstanceState(Bundle) method is onSaveInstanceState(Bundle) before placing the activity in such a background state, allowing you to save any dynamic state of the instance in your activity in this Bundle, so that you can later get it in onCreate(Bundle) if the activity needs to be recreated. For more information about how the life cycle of a process is tied to the activities that it hosts, see the section "Life Cycle Process". Note that it is important to store persistent data in onPause() instead of onSaveInstanceState(Bundle) , because the latter is not part of the lifecycle callbacks, so it will not be called in every situation, as described in its documentation.

Yes onPause() is called before onSaveInstanceState(Bundle) . But onPause() guaranteed to be called part of its life cycle activities .

Usually, when your activity is recreated, for example, when changing the orientation of the device, then onSaveInstanceState(Bundle) is called if you did not specify the android:configChanges in your manifest.xml file.

+1
source

I do not agree with the previous answer. According to the documentation :

When called, this method will execute before onStop (). No guarantees whether this will happen before or after onPause ().

+2
source

All Articles