Search code examples
androidfragmentback-stack

Show Android Fragment only a single time


I have an android app with a main activity, a LoadingFragment, and a TryAgainFragment.

What I try to do is to display in the following order:

defaultView --> display LoadingFragment --> onLoadingFailed: display TryAgainFragment

This works.

Now if the app is showing the TryAgainFragment and the user presses the back-button the following should happen:

TryAgainFragment --> display defaultView

What actually happens:

TryAgainFragment --> **display LoadingFragment** --> display defaultView

I searched quite a while now and can't find anything...

Best regards!


Solution

  • I guess when it fails, you do not need LoadingFragment anymore. When you open TryAgainFragment you can remove LoadingFragment before adding TryAgainFragment.

    You can do it like below.

    getFragmentManager().popBackStack();
    getFragmentManager().beginTransaction().add(tryAgainFragment).addToBackStack(null).commit