Search code examples
androidscreen-orientationandroid-screen

Android : Save application state on screen orientation change


I have seen the following links before posting this question

http://www.devx.com/wireless/Article/40792/1954

Saving Android Activity state using Save Instance State

http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html

How to save state during orientation change in Android if the state is made of my classes?

I am not getting how should i override the following function :

@Override
    public Object onRetainNonConfigurationInstance() {
        return someExpensiveObject;
    }

In my application i have layout with one editext visible and other editext get visible when the data of first editext validates to true.I have set the visbility of all other editextes and textviews to false and make them visible after validating.

So in my activity if the screen orientation is changed then all the items having android:visibility="false" get invisible.

I have also came to know that when our activities screen orientation changes it calls onStop() followed by onDestroy() and then again starts a fresh activity by calling onCreate()

This is the cause .. But i am not getting how to resolve it ..

Here You can see the screenshots of my application :

enter image description here in this image all fields are loaded and in another image when the screen orientation is changed to landscape they are all gone

enter image description here

Any link to tutorial or piece of code will be highly appreciable.

And also my application crashes when a progress dialog is shown up and i try to change screen orientation.How to handle this ??

Thanks


Solution

  • Well if you have the same layout for both screens then there is no need to do so just add below line in your manifest in Activity node

    android:configChanges="keyboardHidden|orientation"
    

    for Android 3.2 (API level 13) and newer:

    android:configChanges="keyboardHidden|orientation|screenSize"
    

    because the "screen size" also changes when the device switches between portrait and landscape orientation.

    From documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html