Don't reload activity and put fragments in correct order when orientation changes

375 views Asked by At

I use framgents in my app and I have a dual pane layout for both landscape (left and right) and portrait (up and down) on a tablet.

I have an activity which loads a fragment into the left selection pane and then you can choose several criteria in some Spinner to do a search. In right fragment is being shown results from search in a ListView.

If I use android:configChanges="orientation" in this activity in Manifest.xml the results from search are saved but I have both pane layouts up and down in landscape mode instead of left and right, as it should be. And if I do not use android:configChanges="orientation" I have both pane layouts in order correct (left and right in lanscape mode) but I do not have results from search, activity restarts.

How I can solve it? Any idea?

Thanks so much.

1

There are 1 answers

0
Lukos On

You should be using onSaveInstanceState() and onRestoreInstanceState()

You need your view to be recreated for the layout to look correct but on recreation the data will be lost. You must save this data using the methods I mentioned or by using the shared preferences (for saving data longer that lasts after application exit).

Do not use android:configChanges="orientation". This prevents the layout from being recreated and expects you to deal with it yourself.

See the documentation for more info: http://developer.android.com/training/basics/activity-lifecycle/recreating.html http://developer.android.com/reference/android/content/SharedPreferences.html