简体   繁体   中英

Saving the state of the screen when orientation is changed

I'm displaying the current location address on the screen. I was told to add (android:configChanges="keyboardHidden|orientation") in manifest file to store the context(address).It works fine,but after adding this line, the screen orientation is not changing . If i remove this line, then the orientation works good but the address disappears on changing the orientation. If anyone knows how to solve this, then pls comment on it.

Thank you.

在清单中使用android:configChanges =“orientation | screenSize”

you need to save the data, as the orientation gets changed the screen is drawn new. ie onDestroy gets called and then onCreate() gets called again. The following link has more info on how to save the data.

Saving Android Activity state using Save Instance State

Restoring state of TextView after screen rotation?

When you rotate the screen, Android redraws the entire activity to for the new orientation. This involves calling onCreate() again, which is why your address is disappearing. Take a look at onSaveInstanceState , an Activity method you can override to store anything you need to hang on to when the activity is closed and reopened. Just package up your data in the Bundle, and then you have access to it again in the Bundle in onCreate().

This sounds overly complex, but this is the correct way to handle it. Android can close your activity for any number of reasons - the screen rotation is just a really easy one to test. If it works for the rotation, then it'll likely work for other situations like low memory, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM