开发者

Saving the state of the screen when orientation is changed

开发者 https://www.devze.com 2023-02-26 10:28 出处:网络
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 aft

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 goo开发者_JAVA技巧d but the address disappears on changing the orientation. If anyone knows how to solve this, then pls comment on it.

Thank you.


Use android:configChanges="orientation|screenSize" in manifest


you need to save the data, as the orientation gets changed the screen is drawn new. i.e. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消