Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to NOT save view state in Android?

I am developing app, where I need to display suggestions in ListView and when orientation changed I want that list to be hidden. But it appears on the screen anyway. I tried this: 1) Get list adapter and clear it. 2) Set visibility GONE before orientation changed (but it becomes visible again after onCreate) 3) Set visibility GONE in onCreate, but the list still appears on the screen (I think that maybe android saves old list, because I initialize list view again and it looks like empty, but the list on the screen isn't). So, how can i get rid of the list?

like image 738
Semyon Danilov Avatar asked Apr 24 '13 12:04

Semyon Danilov


People also ask

What is saved instance state android?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

How do you save onSaveInstanceState?

1 Answer. Show activity on this post. You can replace above with the data you want to save and then re-populate your views like RecyclerView etc with the data you saved in the savedInstance as key-value pair. Just keep in mind that the Bundle is not meant to store Large amount of data.

How are saved bundles retrieved?

Restore your activity state When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes to your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

How do you restore the state of an Android activity?

State can be restored in either the onCreate() or the onRestoreInstanceState() methods of the activity by extracting values from the Bundle object and updating the activity based on the stored values.


Video Answer


1 Answers

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

Controls whether the saving of this view's state is enabled (that is, whether its onSaveInstanceState() method will be called).

If you set it to false it should always go back to the default state when changing orientation because its state won't be saved.

You could, for example, put this in the 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 put the visibility and the method setSaveEnabled in the onCreate method.

I tried with a simple ListView and a Button that changes the visibility to true. On rotation the ListView became invisibile(its default state)

Also note that:

This flag can only disable the saving of this view; any child views may still have their state saved.

so you should have to clear the list during onStop() or whatever method you want but if even if you don't, the ListView will still be invisible on rotation

Source:

  • http://developer.android.com/reference/android/view/View.html#attr_android:saveEnabled
  • http://developer.android.com/reference/android/view/View.html#setSaveEnabled%28boolean%29
like image 132
fabiolbr Avatar answered Nov 15 '22 17:11

fabiolbr