When are ALL the cases when the onSaveInstanceStat

2020-02-10 17:16发布

All the sources I read have all mentioned couple of cases and concluded with "a few other cases". What are ALL the cases when the onSaveInstanceState method called in a View/Activity?

标签: java android
7条回答
▲ chillily
2楼-- · 2020-02-10 17:35

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

查看更多
beautiful°
3楼-- · 2020-02-10 17:40

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.

查看更多
对你真心纯属浪费
4楼-- · 2020-02-10 17:40

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.

查看更多
forever°为你锁心
5楼-- · 2020-02-10 17:42

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.

查看更多
欢心
6楼-- · 2020-02-10 17:46
  • 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.

查看更多
Root(大扎)
7楼-- · 2020-02-10 17:52

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

查看更多
登录 后发表回答