Skip to content Skip to sidebar Skip to footer

Backstack Fragment Not Appearing In Front On Back Button Prerssed

I have gone through many stackoverflow question before writing this. i am so confused about this guy Backstack in fragment. I have Added three fragment on the same container inside

Solution 1:

calling replace() will remove the previous fragment, Fragment 1 should be called using replace(), and Fragment 2 & 3 should be called using add(), you should also add the last transaction to back stack (calling Fragment 3)

Like this:

Fragment 1:

privatevoidaddLandingFragment() {
    landingPageFragment = LandingPageFragment.newInstance();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace( R.id.container, landingPageFragment, LANDING_PAGE_FRAGMENT_TAG );
    transaction.commit();

}

Fragment 2:

publicvoidaddIntrofragment() {
fragment2 = IntroFragment.newInstance();
FragmentTransaction transaction = manager.beginTransaction();
transaction .hide(LandingPageFragment.this);
transaction.add( R.id.container, fragment2, INTRO_PAGE_FRAGMENT_TAG);
transaction.addToBackStack(fragment2.getClass().getName() );
transaction.commit();

}

Fragment 3:

publicvoidonGetStartedClicked() {
    fragment3= ConnectFragment.newInstance();
    FragmentTransaction transaction = manager.beginTransaction();
    fragmentManager.popBackStackImmediate();     // to remove fragment 2    
    transaction.add( R.id.container, fragment3,CONNECT_PAGE_FRAGMENT_TAG );
    transaction.addToBackStack(fragment3.getClass().getName() );
    transaction.commit();
}

finally your onBackPressed should be like this:

@Override
publicvoidonBackPressed() {
    fragmentManager.popBackStackImmediate();
    fragmentTransaction.show(LandingPageFragment.this);
}

therefore your onBackPressed will always pop the top fragment on the stack (Fragment 3), and since Fragment 2 was already popped before adding Fragment 3, then onBackPressed will display the very first fragment.

Solution 2:

Thanks Silvia.H for you support. I have solved my problem and found it as the best possible solution for me.

The only mistake I did was, I did not add fragment3 in backstack

So the only changes required was

publicvoidonGetStartedClicked() {
        fragment3= ConnectFragment.newInstance();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace( R.id.container, fragment3,CONNECT_PAGE_FRAGMENT_TAG );
        transaction.addToBackStack(ConnectFragment.class.getName() );
        transaction.commit();
    }

Now this makes you clear that in order to use popBackStack with name like

manager.popBackStack(fragment2.getClass().getName() ,FragmentManager.POP_BACK_STACK_INCLUSIVE );

you have to keep that transaction in backstack from where you are actually pressing the back button.

I have made one more small change in onBackPressed() method which allows the app to exist when user presses back button on fragment1.

My onBackPressed() look like this now

@Override
    public void onBackPressed() {

        if( manager.getBackStackEntryCount() > 0 ) {
            getSupportFragmentManager().popBackStack(
                    scoreTrackerIntroFragment.getClass().getName(),
                    FragmentManager.POP_BACK_STACK_INCLUSIVE );
        }else {

            super.onBackPressed();
        }

    }

Woop! Now I am clear about this "Backstack" guy.

Post a Comment for "Backstack Fragment Not Appearing In Front On Back Button Prerssed"