Function of the saveInstanceState parameter in the onCreate method, onsaveinstancestate
Sample Code:
Public class MainActivity extends ActionBarActivity {private static final String TAG = "MainActivity"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); if (null! = SavedInstanceState) {int intType = savedInstanceState. getInt ("intType"); String stringType = savedInstanceState. getString ("stringType");} setContentView (R. layout. activity_main); Log. I (TAG, "onCreate ---- executed --------") ;}@ Override protected void onRestoreInstanceState (Bundle savedInstanceState) {super. onRestoreInstanceState (savedInstanceState); int intType = savedInstanceState. getInt ("intType"); String stringType = savedInstanceState. getString ("stringType"); Log. I (TAG, "onRestoreInstanceState ---- executed --------") ;}@ Override protected void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); outState. putInt ("intType", 0); outState. putString ("stringType", "saved"); Log. I (TAG, "onSaveInstanceState ---- executed --------");}}
When will the onSaveInstanceState method be executed?
1. When the user presses the HOME key.
This is obvious. The system does not know how many other programs you want to run after you press HOME. Naturally, it does not know whether acitemedia will be destroyed. Therefore, the system will call onSaveInstanceState, this gives users the opportunity to save some non-permanent data. The following analyses follow this principle.
2. Press the HOME Key and select to run other programs.
3. Press the power button to close the screen display.
4. Start A new activity from activity.
5. When switching the screen direction.
When will the onRestoreInstanceState method be executed?
Activity A "indeed" is destroyed by the system. If it is only possible, this method will not be called. For example, when acitivty A is being displayed, the user presses the HOME Key to return to the main interface, and then the user returns activity A immediately. In this case, activity A is generally not destroyed by the system because of memory, therefore, the onRestoreInstanceState method of activity A will not be executed.