Skip to content Skip to sidebar Skip to footer

Skip Some Fragments Onbackpressed

I am fairly new with android fragments so please bear with me. I have a bunch of fragments using a single activity as host. In my mind, my fragments are grouped by sections althoug

Solution 1:

So the key to your solution here is this guy:

.addToBackStack(null)

Instead of null, you can pass in a String identifier for that particular transaction -- for instance, the class name of the Fragment is what we use (although that doesn't work if you have multiple instances of the same fragment on the backstack):

.addToBackStack(Fragment1.class.getName())

Then, if you wanted to get back to Fragment1 from Fragment3, just pop using the identifier of the next fragment, and pass the INCLUSIVE flag (which means it will also pop that next fragment that you specified):

getFragmentManager().popBackStack(
        Fragment2.class.getName(), 
        FragmentManager.POP_BACK_STACK_INCLUSIVE);

Which will play your animations as expected, but as if the fragments in between were never there. The reason I suggest to use the next fragment is because you probably don't want your first fragment transaction on the back stack.

Solution 2:

You could try this, it should work and doesnt give a split-second delay. Its not beautiful code though, and if somebody has a better way, please post.

1.Give a tag to your fragments.

transaction.add(R.id.main_activity_container, FirstFragment, "FirstFragment");
transaction.replace(R.id.main_activity_container, Second, "SECOND");
transaction.replace(R.id.main_activity_container, Third, "THIRD");

2.Modify your onBackPressed() in your frameActivity (activity) that "houses" your fragments.

@OverridepublicvoidonBackPressed() {
    // TODO Auto-generated method stubintlastStack= getSupportFragmentManager().getBackStackEntryCount();
    try {
        //If the last fragment was named/tagged "three"if (getSupportFragmentManager().getFragments().get(lastStack).getTag().equalsIgnoreCase("THIRD")){

            getSupportFragmentManager().popBackStackImmediate();
            getSupportFragmentManager().popBackStackImmediate();

            //Get your first fragment that you loaded in the beginning. Fragmentfirst= getSupportFragmentManager().findFragmentByTag("FirstFragment");
            FragmentTransactiontransaction= getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.main_activity_container, first);
            transaction.commit();
            return;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    super.onBackPressed();
}

Solution 3:

When you display Frag2 use:

finalFragmentTransactionfragmentTransaction= getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(FRAG_2);
fragmentTransaction.replace(containerID, frag2, FRAG2_TAG);

and on back press:

@OverridepublicvoidonBackPressed() {
   finalFrag2frag2= (Frag2)getSupportFragmentManager().findFragmentByTag(FRAG2_TAG);
   if (frag2 != null && frag2.isVisible() && getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStackImmediate();
    return;
   }
}

This will prevent from frag2 to be displayed after onBackPressed() called. Avoid using popBackStack() as this will results in frag2 lifecycle trigger (onCreate, onStart, onResume ...)

Post a Comment for "Skip Some Fragments Onbackpressed"