ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Pop to the current fragment in the stack #39

Closed zezekalo closed 7 years ago

zezekalo commented 7 years ago

I've got a question: if I have a fragment backstack and want to pop to particular fragment in this backstack. Is there any possibility to find this particular fragment and return to this point within current backstack?

My code for this: String accountTag = AccountsFragment.class.getSimpleName(); AccountsFragment fragment = (AccountsFragment) getSupportFragmentManager().findFragmentByTag(accountTag);

    if (mainPresenter.hasLocallySavedUsers()) {
        if (fragment != null) {
            getSupportFragmentManager().popBackStackImmediate(accountTag, 0);
        } else {
            String welcomeTag = WelcomeFragment.class.getSimpleName();
            WelcomeFragment welcomeFragment = (WelcomeFragment) getSupportFragmentManager().findFragmentByTag(welcomeTag);
            if (welcomeFragment != null) {
                getSupportFragmentManager().popBackStackImmediate(welcomeTag, 0);
            } else {
                startAccounts(accountsButton);
            }
        }
    } else {
        startAccounts(accountsButton);
    }
ncapdevi commented 7 years ago

Currently there is not. It works as a FIFO stack which is how most navigation is recommended. It seems like fragments randomly being removed from below the stack would provide for a chaotic user experience. You may want to consider a slightly different design pattern (maybe using an Activity to manage your onboarding/welcoming, and finishing the activity as you move forward). You could also fragNav.ClearStack() and then replace the bottom fragment, this may be similar to what you're looking to do.