denno
denno

Reputation: 139

Android Fragments switch without destroy

I have an activity with 3 fragments. Every fragment has a question. When i create the activity I'm calling .add(...) method for the first fragment. For the others i call .replace(...) method with addToBackStack(...) method.

So i have this situation in the backstack:

.add(Frag1)

--> Frag 1

.replace(..., Frag2, ...)

-->Frag2

-->Frag1

.replace(..., Frag3, ...)

--> Frag3

--> Frag2

--> Frag1

Now, when I'm at the third fragment and then go back I'm popping it from the stack. I want to preserve the third fragment so if i go back to the second and then go next to the third i want to have the question answered and not to call again another onCreate().

I want to create a sort of switch of the fragments. Is it possible? Can someone help me?

Upvotes: 0

Views: 2334

Answers (2)

whd.nsr
whd.nsr

Reputation: 694

How about you use viewpager instead, you will need to:

  • Disable paging by swipe, so you can change the page programmatically
  • Load all pages at once (set setOffscreenPageLimit your pages count), so that each page keeps its state

Upvotes: 0

Atef Hares
Atef Hares

Reputation: 4881

You can create a fragment once and Show/Hide it depending on your needs.

See hide & show from FragmentTransaction docs

Also see this question

Upvotes: 3

Related Questions