Saving Android activity variables

Source: Internet
Author: User
1. Call Sequence of callback functions of two activities when one activity AA starts another activity AB:

1. The onpause () of AA is called;
2. The oncreate (), onstart (), and onresume () calls of AB are called sequentially. At this time, AB obtains the user focus;
3. If AA is invisible to the screen (the AB interface is not full screen), The onstop () of AA is called.

It can be extended from the above: if the data modified in AA needs to be used in AB, then it needs to be used in onpause () save the modified data to the database (or other space that can be shared by two activities), instead of onstop.
Ii. Saving temporary variables:
When onpause () and onstop () are called, they may be destroyed by the system due to insufficient memory. Therefore, you need to save some data, to prevent activity from being killed and lost:
Some data that needs to be saved to the database can be re-loaded from the database in the oncreate () method;
Some data that cannot be saved to the database (the database does not set the corresponding fields for this part of the data, for example, data only used to identify the status information of the current activity) through onsaveinstancestate (bundle) when the system rolls back to this activity again, the system obtains the previously saved data from the bundle in oncreate (bundle, you can also use the onrestoreinstancestate (bundle) method (called after the onstart () method) to obtain and set the stored data;
For data that cannot be recorded with bundle, you can save the data in object onretainnonconfigurationinstance () and use the getlastnonconfigurationinstance () method to obtain the objects returned by the previous function, set the object according to the object (the restoration operation can be performed in the oncreate () and onstart () methods ).
Iii. Differences between onsavdinstancestate (bundle) and onpause:
1. When another activity is to be started and the current activity loses focus, the former is called first and then the latter;
2. When you click back or call finish () to end the current activity, the former will not be called (call sequence: onpause ()-> onstop () -> ondestory ()).
4. When some activity configurations (such as the screen direction) change, the system restarts the activity by default. You can also handle some configuration change events by yourself:
<Activity Android: Name = ". myactivity"
Android: configchanges = "orientation | keyboardhidden"
Android: Label = "@ string/app_name">
Like the above Android: configchanges configuration, when these configuration changes, the system will not restart the activity, which is the system will call the onconfigurationchanged () method.
We can implement this method as needed, as shown below:
@ Override
Public void onconfigurationchanged (configuration newconfig ){
Super. onconfigurationchanged (newconfig );

// Checks the orientation of the screen
If (newconfig. Orientation = configuration. orientation_landscape ){
Toast. maketext (this, "Landscape", Toast. length_short). Show (); // Of course the actual processing should not be like this
} Else if (newconfig. Orientation = configuration. orientation_portrait ){
Toast. maketext (this, "portrait", Toast. length_short). Show ();
}
// Checks whether a hardware keyboard is available
If (newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_no ){
Toast. maketext (this, "keyboard visible", Toast. length_short). Show ();
} Else if (newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_yes ){
Toast. maketext (this, "keyboard hidden", Toast. length_short). Show ();
}
}

Related Article

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.