开发者

When are ALL the cases when the onSaveInstanceState() method called?

开发者 https://www.devze.com 2023-03-19 19:09 出处:网络
All the sources I read have all mentioned couple of c开发者_运维百科ases and concluded with \"a few other cases\". What are ALL the cases when the onSaveInstanceState method called in a View/Activity?

All the sources I read have all mentioned couple of c开发者_运维百科ases and concluded with "a few other cases". What are ALL the cases when the onSaveInstanceState method called in a View/Activity?


onSaveInstanceState() will be called by default for a view if it has an id.

google said: "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".

More info here.


Whenever there is as soft kill of the activity. I.e when the orientation changes or when the process is killed by android due to low memory.

It's not called when the user knowingly just navigates away from the activity.

Refer to this link: https://sites.google.com/site/jalcomputing/home/mac-osx-android-programming-tutorial/saving-instance-state


The doc says

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.


Also be aware that onSaveInstanceState can be called on a fragment directly after onCreate (onCreateView, onActivityCreated, onStart, and onResume will NOT be called), if the fragment is attached to an activity but not shown, then destroyed. Thus you need to be sure that everything you reference in onSaveInstanceState is initialized in onCreate, otherwise you risk a NullPointerException.


  • onSaveInstanceState() is called when there is an orientation change or user presses home button.
  • If there is an another activity in front of an activity and the OS kills the hidden activity in order to free memory(or when memory is needed elsewhere), then onSaveInstanceState() is called so activity can save its state information which is restored using onRestoreInstanceState() when user start that activity next time .
  • Default views of Android save their state via a call to View.onSaveInstanceState which is restored by the default implementation of onRestoreInstanceState

As per doc

If the user interacts with an activity and presses the Back button or if the finish() method of an activity is called, the activity is removed from the current activity stack and recycled. In this case there is no instance state to save and the onSaveInstanceState() method is not called.

If the user interacts with an activity and presses the Home button, the activity instance state must be saved. The onSaveInstanceState() method is called. If the user restarts the application it will resume or restart the last running activity. If it restarts the activity it provides the bundle with the save data to the onRestoreInstanceState() and onCreate() methods.

If you override onSaveInstanceState() and onRestoreInstanceState() you should call the super implementation of it, because the default views of Android store their data via a call to View.onSaveInstanceState from the onSaveInstanceState() method of the activity. For example EditText stores its content via the default call of this method.


This method did not call when user presses "return" button,this is one of that case..


onSaveInstanceState is called when ever activity is out of veiw.. like when u press home key, onSaveInstanceState is called.


From here: the answer is onSaveInstanceState() gets called regardless of whether or not your app process is killed. So in all of these scenarios, onSaveInstanceState() is called:

Normal background scenario

  1. Your activity goes into the background and onSaveInstanceState() is called
  2. You do some other things
  3. Your user navigates back to your activity

Process kill background scenario

  1. Your activity goes into the background and onSaveInstanceState() is called
  2. You do some other things and during this time the system starts running low on resources and closes your app’s process
  3. Your user navigates back to the activity onRestoreInstanceState() and onCreate() (with the saved instance state bundle) are called

Configuration change scenario

  1. A configuration change occurs and onSaveInstanceState() is called
  2. onRestoreInstanceState() and onCreate() (with the saved instance state bundle) are called

The difference is whether onCreate()/onRestoreInstanceState() are called. In the process kill and configuration scenarios they are called. In the normal scenario, there is no need to recreate the activity and neither are called. Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed. This is because in the scenario your app process gets killed, the system is pretty resource constrained as is and it would NOT be a good time to spend cycles storing key/value pairs in RAM.

0

精彩评论

暂无评论...
验证码 换一张
取 消