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?
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.
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With