About Onsaveinstancestate and Onrestoreinstancestate ()

Source: Internet
Author: User

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 ()

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.