The reason for this is that the work has encountered two problems.
One problem is that the page is blank and fragment is created repeatedly.
Another problem is that the login page uses Autocompletetextview, and calling the Showdropdown () method causes crash.
1. Onsaveinstancestate () and onrestoreinstancestate () do not match 2. Trigger timing
Onsaveinstancestate ()
(1), when the user presses the home button.
(2), long press the home key, choose to run other programs.
(3), Press the power button (turn off the screen display).
(4), when starting a new activity from activity a.
(5), the screen direction when switching, such as from the vertical screen when switching to a horizontal screen.
Onrestoreinstancestate ()
Onrestoreinstancestate () is invoked on the premise that activity a "does" have been destroyed by the system.
3. Default implementation
Almost all UI controls defined in the Android application framework implement the Onsaveinstancestate () method appropriately, so these UI controls automatically save and restore state data when activity is destroyed and rebuilt.
For example, the EditText control automatically saves and restores the input data, and the CheckBox control automatically saves and restores the selected state
Developers just need to specify a unique ID for these controls (by setting the Android:id property), and the rest can be done automatically. If no ID is specified for the control, the control does not perform automatic data saving and recovery operations.
4. How to test
android:screenorientation= "Portrait"
Remove, support to turn the screen. Onsaveinstancestate () and Onrestoreinstancestate () are then bound to be triggered.
5. Practice Notes
1. Blank page problem,
A new fragment was created when OnCreate.
Then Onrestoreinstancestate (), the old fragment reloaded in.
Workaround:
Onsaveinstancestate (). Save the fragment in the Fragmentmanager.
When OnCreate, determine whether the savedinstancestate is empty. If it is not empty, the description calls Onrestoreinstancestate (), takes the old fragment directly, and creates a new one if it is empty.
Please refer to the daily WiFi items for specific codes. Mainactivity?. Java
2.AutoCompleteTextView leads to crash problems.
The reason is simple. At Onrestoreinstancestate (), the Autocompletetextview SetText () method is called, and then Showdropdown () is called crash.
Because this time the activity has not attach to the window to go up.
Workaround:
Increase the condition judgment. If the activity is not attach to the window at this time, return.
Specific codes refer to daily WiFi items. Userloginactivity?. Java
Reference
- ? Instancestate of Android Development
About Onsaveinstancestate and Onrestoreinstancestate ()