Gonzalo Solera
Gonzalo Solera

Reputation: 1192

How to not delete fragments in Scrollable tabs + swipe

I have an scrollable tabs + swipe activity with 3 different fragments on it. It works well but I don't want my fragments to be destroyed when I navigate between them. For example, when I create the activity, it's shown the first fragment but it's created the second one too. When I swipe to the second fragment, the third is created, so I can swipe to the third one and it will be created before I open it. When I return to the second one, my third fragment still stays created but when I return to the first one, the third fragment is deleted.

There would be any way to avoid this? I don't want my fragments to be deleted when I navigate between them.

Upvotes: 0

Views: 235

Answers (2)

D-Kent
D-Kent

Reputation: 142

This answer should help you figure out what's going on. Pretty much you are most likely using a FragmentStatePagerAdapter, when you should be using a FragmentPagerAdapter. That keeps it from deleting fragments not in view.

If, for performance reasons, you need to have the fragments deleted when they aren't being used, there are other options. Either look at why the performance is that bad when you have only 3 tabs (It shouldn't be unless you are doing something wrong with a listview, or have a map open, etc.) or you can code the fragments to save their state when they are being destroyed (which should be happening anyways with a FragmentStatePagerAdapter).

If that doesn't help, we'll need more info on what you are doing exactly to help.

Upvotes: 1

Gak2
Gak2

Reputation: 2701

If you use a ViewPager, it has a method called setOffscreenPageLimit(int limit). Try and see if setting that higher works

Upvotes: 2

Related Questions