Save & Restore State with the previous example of Android Apidemo example parsing (9): App->activity->persistent state implements a similar UI, but with slightly different functionality and implementation methods.
(9) The value of EditText in the UI is maintained through shared Preferences and Activity's OnPause (), and Onresume ().
This example is done by Onsaveinstancestate (Bundle savedbundle) to maintain the UI state. Unlike Onpause,onresume, Onsaveinstancestate is not part of activity life cycle, and does not necessarily occur before activity is destroyed, for example, when a user presses "back" When the key is rolled back to the previous activity, the current activity does not call the Onsaveinstancestate method because the user indicates that he wants to close the current activity, and OnPause is bound to do so, because OnPause is a state of the activity life cycle. If Onsaveinstancestate is called, it must be called before OnStop, sometimes before onpause.
Onsaveinstancestate is designed to be called before activity destroy, such as when Android switches to another app, when Android is cleared with activity that might be running in the background, depending on the allocation of system resources. Or the system configuration changes, such as the screen from the vertical level, the current activity will be destory first, and then based on the current configuration re-create the current activity instance.
At this point, the activity's onsaveinstancestate is called before activity destroy, and in this case, some UI states should be temporarily stored in the bundle in Onsaveinstancestate. The Android system then re-executes the activity's OnCreate method, passing in the parameter as the bundle object. If set at Onsaveinstancestate, the bundle object is a previously saved bundle object that can be used in OnCreate or onrestoreinstancestate (bundle Savedinstancestate) Restores the state of the UI so that it can remain on when the app or activity restarts.
But look at Saverestorestate.java there seems to be no code associated with Save/restore state, methods Getsavedtext and Setsavedtext are irrelevant to this example.
This is because the activity's default Onsaveinstancestate implementation completes the save and restore of the UI state. in the example above, the EditText input will be restored when the activity is restarted, and the following edittext input will not be saved and restored. the activity's default Onsaveinstancestate implementation only implements save and restore for the view that defines the ID (android:id). Let's look at the definition in R.layout.save_restore_state:
<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent" android:layout_height= "match_parent" > <linearlayout android:orientation= "vertical" Android: Padding= "4dip"Android:layout_width= "Match_parent" android:layout_height= "wrap_content" > <textview android:id= "@+id/msg"Android:layout_width= "Match_parent" android:layout_height= "Wrap_content"Android:layout_weight= "0" android:textappearance= "? Android:attr/textappearancemedium"Android:paddingbottom= "4dip"/> <TextView android:layout_width= "Match_parent" android:layout_height= "Wrap_content"Android:layout_weight= "0" android:paddingbottom= "4dip"android:textappearance= "? Android:attr/textappearancemedium"Android:text= "@string/saves_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:textappearance= "? Android:attr/textappearancemedium" android:text= "@string/initial_te XT " Android:freezestext=" true "> </EditText><TextView android:layout_width= "Match_parent" android:layout_height= "Wrap_content"Android:layout_weight= "0" android:paddingtop= "8dip" android:paddingbottom= "4dip"android:textappearance= "? Android:attr/textappearancemedium"Android:text= "@string/no_saves_state"/><EditText android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_weight= "1" android:background= "@drawable/red" android:textappearance= "? Android:attr/textappearancemedium " android:text=" @string/initial_text "> </edittext></LinearLayout></ScrollView>
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: Enter values in the upper and lower edittext, then rotate the screen, and you can see that only the above EditText values are preserved.
Note: On emulator, you can press "NumLock" first, then press 7, 9来 rotate the screen.
In some cases you need to overload onrestoreinstancestate, so don't forget to call super.onrestoreinstancestate.
"Sail Plan 012" 2015 sail plan Android Apidemo Devil Pace One App->activity->save & Restore State onsaveinstancestate Onrestor Einstancestate