Skip some fragments onBackPressed

前端 未结 3 1489
[愿得一人]
[愿得一人] 2021-02-05 16:28

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

相关标签:
3条回答
  • 2021-02-05 16:38

    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.

    0 讨论(0)
  • 2021-02-05 16:50

    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.

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
    
        int lastStack = 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. 
                Fragment first = getSupportFragmentManager().findFragmentByTag("FirstFragment");
                FragmentTransaction transaction = 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();
    }
    
    0 讨论(0)
  • 2021-02-05 17:04

    When you display Frag2 use:

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

    and on back press:

    @Override
    public void onBackPressed() {
       final Frag2 frag2 = (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 ...)

    0 讨论(0)
提交回复
热议问题