Search code examples
androidandroid-fragmentsfragmentfragmenttransaction

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?


Solution

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

    See hide & show from FragmentTransaction docs

    Also see this question