Function of the saveInstanceState parameter in the onCreate method, onsaveinstancestate

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.