Onsaveinstancestate () and Onrestoreinstancestate ()
Full life cycle of activity
onCreate()–> onStart() –> onRestoreInstanceState() –> onResume() –> onSaveInstanceState() –> onPause() –> onStop() –>onDestroy()
The two methods of onsaveinstancestate and Onrestoreinstancestate are known to be Activity the preservation of data for destruction and reconstruction.
- Press the back key or call the finish () method to actively destroy the activity , this time the system will think that we no longer need it, the
Activity system will not execute Onsaveinstancestate .
- Press the home key to direct the program backstage, this time the system will execute onsaveinstancestate () The system knows that you do not need this activity just backstage.
At this point we wake up the application and do not execute the onrestoreinstancestate method, because we do not destroy the rebuild after we wake up in the background Activity , so we do not call Onrestoreinstancestate .
- Press the home key will execute onsaveinstancestate , and then the system because of insufficient memory to kill the process, this time the system feel that they do wrong, to give you a state of recovery, when we start the program again will be executed Onrestoreinstancestate This method to restore data to us.
- In the default
Activity , if the system will be destroyed and re-created when the screen is switched Activity , the system will execute onsaveinstancestate and onrestoreinstancestate , because this is the system to be Activity destroyed, the system is responsible for executing these two methods to give you to save and restore data.
Methods to execute when destroying and rebuilding data