Real-time data storage for Android learning notes-onsite protection onsaveinstancestate ()

Source: Internet
Author: User

Data preservation: In the software development we want to save the activity data, in order to achieve the customer data storage, to achieve a better user experience. Then we need to solve the following problems: 1. When to save?    2. What data is saved? I want to save the data generated by the application, which is generated when the activity interacts with the user, that is, the data on the interface, or the state. 3. How do I save? 4. Where to save? 5. Write the appropriate example? Create ideas, where to look for answers, first of all, I want to save the data generated by the application, and the data of these users is generated when the activity interacts with the user, so we go into activity to find. Activity Source:
* <ul> * <li>* Importantly, here you'll usually call {@link #setContentView (int*  ** programmatically.

OnCreate is initializing your activity, where you can call Setcontentview (int) and the layout resource to define your UI interface. Use Findviewbyid to remove the controls you interact with

* <li> this ** * </ul>

The meaning of this sentence is that OnPause is a way of handling the user's departure. Most importantly, the data generated by any user operation should be committed here, using Android.content.ContentProvider} to save the data.

Here we can conclude: 1. When to save? I. Save operation at OnPause 3. How do I save it? Use the ContentProvider class for processing. What's the deal? Continuing to read this, we're going to come back and see Android.content.ContentProvider we go into contentprovider, but we're dealing with shared data, so our conclusion is not the best way. {@link Android.app.activity#onpause} to commit changes to data and * otherwise prepare to stop interacting with the user. Continue reading,
 This if  For more information on how the lifecycle of a process are tied to the activities it is hosting. Note that it was important to save persistent data in OnPause instead of Onsaveinstancestate because the later was not part Of the lifecycle callbacks, so is not being called in every situation as described on its documentation.

we can use Onsaveinstancestate (Bundle) method to store, and thenonRestoreInstanceState 处进行处理,即拿到值。

如何使用Onsaveinstancestate (Bundle) method? We add a text box and a button in layout to override the Onsaveinstancestate method,
protected void  onsaveinstancestate (Bundle savedinstancestate) {      Super . Onsaveinstancestate (savedinstancestate);}   protected void onrestoreinstancestate (Bundle savedinstancestate) {  super. onrestoreinstancestate (savedinstancestate); }

This way the data can be saved when rotated, but it is not possible to jump up using Actionbar, because:

* <p>do not confuse Thismethod with activity lifecycle callbacks such as*{@link #onPause}, which is always called when a activity is being placed*in the background or on its-to destruction, or {@link #onStop} which*is called before destruction. One example of when {@link #onPause} and* {@link #onStop} is called and not Thismethod is while a user navigates back*From activity B to activity a:there are no need to call {@link #onSaveInstanceState}*On B because that particular instance would never be restored, so the*system avoids calling it. An example if {@link #onPause} is called and*Not {@link #onSaveInstanceState} was when activity B was launched in front of activity A:* The system may avoid calling {@link #onSaveInstanceState} on Activity AifIt isn ' t * killed during the lifetime of B since the state of the userInterface of* A'll stay intact.

Do not confuse this method with the activity lifecycle callback such as OnPause () or OnStop (), OnPause () is always called when the activtiy is placed in the background or destroyed by itself, and OnStop () is called when the activity is destroyed. An example that calls OnPause () and OnStop () but does not trigger onsaveinstancestate is when the user returns to activity a from activity B: there is no need to call B's Onsaveinstancestate ( Bundle), the B instance is never restored, so the system avoids calling it. An example of calling OnPause () but not calling Onsaveinstancestate is when activity B starts and is at the front end of activity a: If the user interface state of a is not compromised throughout the life cycle of B, The system does not invoke the Onsaveinstancestate (Bundle) of activity A. (http://blog.csdn.net/ddna/article/details/5123482)

Therefore, this method can not permanently save data, suitable for instantaneous preservation of data. When do I callOnsaveinstancestate method. (Pro-Test) http://www.cnblogs.com/hanyonglu/archive/2012/03/28/2420515.html

(1), when the user presses the home button.

It is obvious that the system does not know how many other programs to run after you press home, and it is not known whether activity A will be destroyed, so the system will call Onsaveinstancestate (), giving the user the opportunity to save some non-permanent data. The following are some of the conditions in which the analysis follows this principle

(2), long press the home key, choose to run other programs.

(3), Press the power button (turn off the screen display).

(4), when starting a new activity from activity a.

(5), the screen direction when switching, such as from the vertical screen when switching to a horizontal screen.

Before the screen is switched on, activity A is destroyed, and activity A is automatically created after the screen switch, so onsaveinstancestate () is bound to be executed and will certainly execute onrestoreinstancestate ().

Special Instructions

If we didn't overwrite the onsaveinstancestate () method, this method automatically saves some data in the activity by default, such as the state and value of the UI control, as long as you need to specify a unique ID for the control. Otherwise, the data for the control does not appear to be saved.

So when do you save the Overwrite method?

If you need to save additional data, you need to overwrite the Onsaveinstancestate () method. It is important to note that the Onsaveinstancestate () method is only suitable for storing transient data, such as the state of the UI control, the value of the member variable, and not the persisted data, which should be persisted when the user leaves the current activity at OnPause () (for example, saving data to a database or file). In this case, it is important to understand that it is not suitable for storing more time-consuming data in OnPause ().

As for the Onrestoreinstancestate method, it should be noted that the Onsaveinstancestate method and the Onrestoreinstancestate method "not necessarily" are called in pairs, Onrestoreinstancestate is invoked only if activity a is "really" destroyed by the system, and if it is just stuck in a situation where there is such a possibility, then the method will not be called, for example, when activity A is being displayed, The user presses the home key back to the main interface, and then the user then returns to activity a, in which event A is not normally destroyed by the system for memory reasons, so activity A's Onrestoreinstancestate method is not executed

This is where we solve our problems:

4. Where to save? Instantaneous, using the system-providedThe Onsaveinstancestate method overrides adding some of the data that the system does not automatically save, while using other variables. require long-term storage, use of databases, files, orsharedpreferences, the cache exists in the catch file. The most important thing is to use several protections based on the distinction between pros and cons. 5. Write the appropriate example? Example I would not write, the simplest way to write a screen rotation, and then save the data, on it, online a. Description: This article refers to some of the other people's articles, after their own testing, thank the writer, remember, the article wrote the main address. Son of a dull, wrong please advise
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.