Steve Benett
Steve Benett

Reputation: 12933

Prevent FragmentStatePagerAdapter to not destroy fragments

I have some Fragments in a List Collection to be used for a Viewpager with a FragmentStatePagerAdapter. Im using only getItem(){return list.get(index;} and getCount(){return list.size()} in it. If i wanna replace that List in the adapter, i load another List with different Fragments, call notifyDataSetChanged() and set the adapter to the viewpager again to make it happen. That is workin fine.

But if i do this fragmentset change the previous Fragments are gettin destroyed with a call of onDestroy() through the PagerAdapter. Now i would like to know if its possible to prevent the onDestroy call? Or i HAVE TO use onSavedInstanceState() to load the previous Fragments and their state again?

Im just searchin some different methods to restore the state of the fragments which are gone because of the change in the adapter. If anyone know something about this it would be nice to share some thoughts. I can give code if its necessary.

Maybe a good Info: I dont use the PagerAdapter to instantiate the Fragments. I do this before i load the Lists to the Adapter.

Upvotes: 2

Views: 2938

Answers (2)

Jooony
Jooony

Reputation: 31

I suggested same problem the ather day.

so I found another solution.

I override destroyItem(ViewGroup container, int position, Object object) in Adapter class.

and changed not working super.destroyItem ..

I'm not sure that solution is better solution, but it's definitely help for me.

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006704

Now i would like to know if its possible to prevent the onDestroy call?

The complete and entire point of using FragmentStatePagerAdapter is to allow Android to destroy fragments that are not in use. You should be using FragmentPagerAdapter instead.

Upvotes: 6

Related Questions