I have an app using fragments, all of which are contained in a single activity. The activity starts with a fragment containing a menu of buttons, all of which cause various lis
how to work around it and maintain the list view/current fragment through the orientation change
You are blindly replacing the fragment every time onCreate()
is called. Instead, only add/replace the fragment if savedInstanceState()
is null
. If it is not null
, you are coming back from a configuration change, and your existing fragments will be recreated (or, if they were retained, they are already there).
setRetainInstance(true)
means that the fragment itself will be retained across configuration changes, instead of being destroyed/recreated like the activity is. However, it will still be called with onCreateView()
. In your code, that means that your data members of ItemListFragment
would stick around, but you would still need to call setListAdapter()
even if you do not requery the database.