[Apidemos for Android samples video series] app-activity-saverestore state

Source: Internet
Author: User

1. Demo description and demonstration

This demo demonstrates onsaveinstancestate (bundlesavedbundle) to maintain the UI state


Results:


2. Video description
Http://www.eyeandroid.com/thread-10735-1-1.html

3. Demo Analysis

 

In this example, the onsaveinstancestate (bundle savedbundle) is used to maintain the UI state. Unlike onpause and onresume, onsaveinstancestate is not part of activity life cycle and may not be executed before activity is destroyed, for example, when you press the "back" button to roll back to the previous activity, the current activity does not call the onsaveinstancestate method, because the user explicitly wants to close the current activity, and the onpause will definitely execute, because onpause is a state of activity lifecycle. If onsaveinstancestate is called, it will be called before onstop and sometimes before onpause.

Onsaveinstancestate is designed to be called before the activity destroy (for example, when Android switches to another application, Android may clear the activity running in the background based on the distribution of system resources, or the system configuration is changed. For example, if the screen is flattened vertically, the current activity is destory first, and then the instance of the current activity is re-created based on the current configuration. At this time, the onsaveinstancestate of the activity will be called before the activity destroy. In this case, the onsaveinstancestate should be used to temporarily store some UIS in bundle. Then, the android system re-executes the oncreate method of the activity and passes in the bundle object as the parameter. If this bundle object is set in onsaveinstancestate, the application can restore the UI state in oncreate or onrestoreinstancestate (bundle savedinstancestate, in this way, you can maintain the status when the application or activity is restarted.

However, it seems that there is no save/restore state-related code in saverestorestate. java. The getsavedtext and setsavedtext methods are not related to this example.

This is because the default onsaveinstancestate of the activity completes the SAVE and restore of the UI state. In this example, the edittext input is restored when the activity is restarted, while the edittext input below is not saved and restored. The default onsaveinstancestate implementation of the activity only implements save and restore for the view with the ID (Android: ID) defined. Let's take a look at the definitions in R. layout. save_restore_state:

<Edittext Android: Id = "@ + ID/saved"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1 ″
Android: Background = "@ drawable/Green"
Android: text = "@ string/initial_text"
Android: freezestext = "true">
<Requestfocus/>
</Edittext>

....
<Edittext
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1 ″
Android: Background = "@ drawable/red"
Android: text = "@ string/initial_text">
</Edittext>

The edittext above defines the ID while the edittext below does not. This can be tested by rotating the screen. Rotating the screen ensures that the current activity is destory first and then create: enter a value in the up and down edittext, and then rotate the screen. You can see that only the edittext value above is retained.

Note: On emulator, you can press "numlock" and then press "7" to rotate the screen.

4. We learned this demo.

Onsaveinstancestate and onrestoreinstancestate

Http://www.eyeandroid.com/thread-10637-1-1.html

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.