0

I have an android app in which I want to dynamically add and remove Fragments to a ViewPager during runtime, and preserving the Fragments on configuration changes.

Thus far I have implemented a FragmentStatePagerAdapter, holding my Fragments in a list with methods add, remove and replace to manipulate my Fragments. Basically, all they do is insert or delete a Fragment in the list and then call notifyDatasetChanged(). I insert the staring Fragment as a new Fragment during the Activitys onCreate() method.

I know want to preserve my Fragments during configuration changes. Currently, when I change my screen orientation, all my currently Fragments are deleted, and I get back to the starting Fragment, naturally because the Activity is recreated, and the Fragment and the Adapater are reinstantiated during the onCreate() call. I was trying to save the state of the Adapter with the Adapter.saveInstance() during the Activitys onPause(), but when I try to restore it via Adapter.restoreInstance(), I get an empty adapter.

Can anyone give me a hint on what is the correct way in Android to save state a FragmentStatePagerAdapter?

Benjamin Scharbau
  • 2,028
  • 1
  • 16
  • 33

1 Answers1

1

To save the state of the current fragment, i think first of all you will have to check which fragment is currently added/visible in the container activity. Once you have that fragment, simply use onSaveInstanceState() method of the container activity to save the instance of the current fragment.

For code snippet please follow this link.

Community
  • 1
  • 1
Kaveesh Kanwal
  • 1,753
  • 17
  • 16
  • Thanks, that was what I was looking for. I had to put it in a for() loop to save all Fragments in my Adapter, but it seems to be working like expected – Benjamin Scharbau Sep 15 '15 at 07:31
  • Well this actually works only for a limited amount of fragments. When I want to save fragments that are away more than 1 screen of my current fragment, I get an "Fragment not in FragmentManager" error. Any hints on that? – Benjamin Scharbau Sep 16 '15 at 01:28
  • In onSaveInstanceState() you will save only that fragment which is currently attached/visible to the user. So, the next time when the user re-creates the activity on screen rotation, he will go back to the same fragment that was last visible. Thus, at a time you will one only one fragment visible in your container activity – Kaveesh Kanwal Sep 16 '15 at 04:03
  • But I need to recreate *all* fragments in my ViewPager – Benjamin Scharbau Sep 16 '15 at 08:08
  • One solution that i can think of is that you save the position of your current view pager fragment in onSaveInstanceState() and once the activity gets re-created you populate your view pager like the way you do and then set it to the position that you had saved in the onSaveInstanceState(). – Kaveesh Kanwal Sep 16 '15 at 09:15
  • You see, that's the problem, I populate my view pager dynamically, so I don't know during onCreate() which Fragments with which arguments I have in my view pager, so I need a way to store that information – Benjamin Scharbau Sep 17 '15 at 01:12