Android Apidemos Sample Resolution App->activity->save & Restore State

Source: Internet
Author: User

Save & Restore State and previous examples The android Apidemo example resolves (9): The UI for app->activity->persistent State implementation is similar, but the functionality and implementation methods are slightly different. (9) The values that are edittext in the UI are maintained through shared Preferences and Activity OnPause (), and Onresume (). This example is achieved by Onsaveinstancestate (Bundle Savedbundle) to preserve the UI state. Unlike Onpause,onresume, Onsaveinstancestate is not part of the activity's life cycle and does not necessarily execute before the activity is destroyed, such as when the user presses "back" When the key is rolled back to the previous activity, the current activity does not invoke the Onsaveinstancestate method, because the user indicates that the current activity is being closed and the OnPause is bound to execute because OnPause is a state of the activity lifecycle. If Onsaveinstancestate is invoked, it must be invoked before OnStop, sometimes before onpause.

Onsaveinstancestate is designed to be called before the activity destroy (for example, when Android switches to another application, when Android uses activity that might be run in the background, based on the allocation of system resources, or system configuration changes, such as the screen from the vertical leveling, the current activity will be destory first, and then recreate the current activity based on the current configuration instance. At this point, the onsaveinstancestate of the activity is invoked before the activity destroy, and some UI state temporary storage should be bundle in onsaveinstancestate. Then the Android system will rerun the activity's OnCreate method, passing the parameter to the bundle object. If set in Onsaveinstancestate, the Bundle object is a previously saved Bundle object that can be applied in OnCreate or onrestoreinstancestate (Bundle Savedinstancestate) to restore the UI state so that you can maintain the status of this exit when the application or activity restarts.

But look at Saverestorestate.java there seems to be no code associated with Save/restore state, methods Getsavedtext and Setsavedtext and this example are irrelevant.

This is because the default Onsaveinstancestate implementation of the activity completes save and restore of the UI state. The EditText input above in the example is restored when the activity restarts, and the following edittext input is not saved and restored. The default Onsaveinstancestate implementation of the activity only implements save and Restore for the view that defines the ID (android:id). Look at the definition 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 above edittext defines the ID and the following edittext does not have an ID defined. This can be tested by rotating the screen. Rotating the screen ensures that the current activity is destory and then create: EditText the input value from top to bottom, then rotate the screen to see that only the EditText value is retained.

Note: On the emulator, you can press "NumLock" first, then press 7, 9来 rotate the screen.

There are some situations where you need to overload onrestoreinstancestate, and don't forget to call Super.onrestoreinstancestate

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.