Android OnPause and Onsaveinstancestate

Source: Internet
Author: User

When a user opens a new activity, the current activity may be in a stopped state in memory or it may be killed by the system because the new activity requires more memory, but anyway, when the user presses the return key on the new activity, What he wants to see is the interface of the original activity. If the original activity was recreated, it will revert back to what the user finally saw it look like. So what do we do? In fact, it is not difficult to keep the necessary data in OnPause () or OnStop () or Ondestyroy () as described in the previous section. But now Google has a new thing: Onsaveinstancestate (), the name of its meaning: it is dedicated to save the instance state, this "instance" is not referring to the activity object, but its process, Because the destruction of activity is caused by the killing of the process in which it is located. Onsaveinstancestate () is called when the system senses the need to kill activity, and it is passed into a parameter: bundle, which can be thought of as a map, a dictionary, and so on, with "key-value" to save the data.

Now it's a pain in the egg: isn't it possible to save data in OnPause ()? Why did you get a guy like that? What about the relationship between the provinces and the wood? The main purpose of the original Onsaveinstancestate () is to preserve data about the state of the activity, and when the system kills the activity, if it wants the activity to appear exactly the same as it is now, Then it calls this onsaveinstancestate (), otherwise it is not called. So understand this: Onsaveinstancestate () is not always called. For example, when a user presses a return on an activity, it is not called, because the user knows at this time that the activity is to be destroyed, and does not expect the next time it will look the same as now (of course, the developer can keep it on the face of death, you have to do this, the system is not), So you don't have to call Onsaveinstancestate (). It should be understood now that in OnPause (), OnStop (), and OnDestroy (), it is necessary to preserve the data that needs to be persisted, rather than to save the state data for the state, which has a special method: Onsaveinstancestate (). The data is stored in a bundle, and the bundle is permanently initialized by the system. When the activity's OnCreate () is called again, the original saved bundle is passed in to restore the last appearance of the last death, or null if the bundle was not saved at the time of the previous die.

Not yet, if you don't implement your own onsaveinstancestate (), but the controls on the activity can still be saved and restored. The activity class has implemented the Onsaveinstancestate (), in the default implementation of Onsaveinstancestate (), will call all the controls related methods, the control of the state are saved, such as the text entered in EditText, checkbox is selected and so on. However, not all controls can be saved, depending on whether you assign a name (Android:id) to the control in the layout file. A well-known existence, no nameless, no matter.

Now that we have a ready-made available, do we still have to implement Onsaveinstancestate ()? This depends on the situation, if your own derived class has variables affecting the UI, or your program's behavior, of course, it is necessary to save this variable, then you need to implement it, otherwise you do not need, But most of the situation definitely needs to be implemented by ourselves. By the right, don't forget to call the parent class's onsaveinstancestate () in your implementation.

Note: Since Onsaveinstancestate () is not called every time it is destroyed, do not save those data that need to be persisted, and the best place to do that is to save the data: OnPause ().

The best way to test the state resilience of your program is to rotate the screen, and whenever the orientation of the screen changes, the current activity is destroyed by the system and recreated (should be the process is closed!). --Won't you? Not high efficiency OH)

Android OnPause and Onsaveinstancestate

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.