Android ApiDemos example resolution (14): App-& gt; Activity-& gt; Save &

Source: Internet
Author: User

Save & Restore State is similar to the previous example Android ApiDemo example parsing (9): App-> Activity-> Persistent State to implement the UI, but the functions and implementation methods are slightly different. (9) The EditText value in the UI is maintained through Shared Preferences and onPause () of the Activity, and onResume. 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.

In some cases, the onRestoreInstanceState needs to be reloaded. Do not forget to call super. onRestoreInstanceState.

Author: mapdigit

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.