How to NOT save view state in Android?

I am developing an application where I need to display sentences in a ListView and when I change the orientation I want this list to be hidden. But he still appears on the screen. I tried this: 1) Install the list adapter and clear it. 2) Set the GONE visibility before changing the orientation (but it becomes visible again after onCreate) 3) Set the GONE visibility in onCreate, but the list is still displayed on the screen (I think that maybe the android saves the old list because I again I initialize the list view and it looks empty, but the list is not on the screen). So how can I get rid of the list?

+7
source share
3 answers

Instead of using something global like android: configChanges that affect ALL views in an Activity, how about using the setSaveEnabled (boolean) method or the equivalent android: saveEnabled xml attribute?

Controls whether or not to save this view state (that is, whether its onSaveInstanceState () method will be called).

If you set it to false, you always need to return to the default state when you change the orientation, because its state will not be saved.

You could, for example, put this in a layout file:

<ListView .... android:visibility="invisible" android:saveEnabled="false"> </ListView> 

and then set the visibility to VISIBLE when you start typing. Or, if you prefer to use the visibility and setSaveEnabled method in the onCreate method.

I tried with a simple ListView and Button that changes the visibility to true. When rotated, the ListView became invisible (its default state)

Also note:

This flag can only disable saving this view; all child views can keep their state.

so you need to clear the list during onStop () or any other method you want, but even if you don't, the ListView will still be invisible when rotated

A source:

+13
source
 @Override protected void onSaveInstanceState(Bundle outState) { } 

override this function in your activity and do not call the "super" method

or indicate the orientation in the manifest:

 <activity android:name="com.blabla" android:configChanges="keyboardHidden|orientation|screenSize" android:screenOrientation="portrait" > </activity> 
+1
source

The Android system calls its onCreate () method of your actiivty when the device orientation changes. Add settings to your activity entry in the manifest if you do not want to re-create your activity when the orientation changes.

 <activity android:name=".MyActivity" android:configChanges="keyboardHidden|orientation" /> 
+1
source

All Articles