Skip to content Skip to sidebar Skip to footer

Android: How Do I Prevent Class Variables Being Cleared On Orientation Change

I have a main activity which has two configurations, one which will display a fragment which is locked to portrait orientation. When a button is clicked on this fragment, a fragmen

Solution 1:

You should use the onSaveInstanceState() and the onRestoreInstanceState() methods of the Activity as mentioned in this link.

Also, you would need to call the setRetainInstance() methods in the Fragments you want to save.

Check out this link for more information about setRetainInstanceState().

Solution 2:

Actually whenever orientation gets changed, activities Oncreate () method is called. So to prevent clearing of variables, override OnConfigurationChange() method and in your manifest file do this at the activity:-

android:configChanges="orientation"

Post a Comment for "Android: How Do I Prevent Class Variables Being Cleared On Orientation Change"