Fragment pressing back button

后端 未结 16 2181
清歌不尽
清歌不尽 2020-11-29 20:18

I am now having an activity containing fragments

[1] , [2] , [3] , [4]

If pressing buttons , [3] , it can be redirected to [4]

I would like to imp

相关标签:
16条回答
  • 2020-11-29 21:04

    In your onCreate() in your activity housing your fragments add a backstack change listener like so:

        fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                List<Fragment> f = fragmentManager.getFragments();
                Fragment frag = f.get(0);
                currentFragment = frag.getClass().getSimpleName();
            }
        });
    

    (Nb. my fragmentManager is declared global) Now every time you change fragment the currentFragment String will become the name of the current fragment. Then in the activities onBackPressed() you can control the actions of your back button as so:

        @Override
        public void onBackPressed() {
    
        switch (currentFragment) {
            case "FragmentOne":
                // your code here 
                return;
            case "FragmentTwo":
                // your code here
                return;
            default:
                fragmentManager.popBackStack();
                // default action for any other fragment (return to previous)
        }
    
    }
    

    I can confirm that this method works for me.

    Update : Kotlin

    override fun onBackPressed() {
    
        when(supportFragmentManager.fragments[0].javaClass.simpleName){
            "FragmentOne" -> doActionOne()
            "FragmentTwo" -> doActionTwo()
            else -> supportFragmentManager.popBackStack()
        }
    }
    
    0 讨论(0)
  • 2020-11-29 21:05

    you can use this one in onCreateView, you can use transaction or replace

     OnBackPressedCallback callback = new OnBackPressedCallback(true) {
                    @Override
                    public void handleOnBackPressed() {
                        //what you want to do 
                    }
                };
        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
    
    0 讨论(0)
  • 2020-11-29 21:06

    What I do in this cases is I implement the onBackPressed() function from the Activity:

    @Override
    public void onBackPressed() {
       super.onBackPressed();
       FragmentManager fm = getSupportFragmentManager();
       MyFragment myFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
    
       if((myFragmen.isVisible()){
          //Do what you want to do
       }
    }
    

    How this works for you too.

    0 讨论(0)
  • 2020-11-29 21:06

    You can use getFragmentManager().popBackStack() in basic Fragment to go back.

    0 讨论(0)
  • 2020-11-29 21:10

    Make sure to add the following:

    if (event.getAction()!=KeyEvent.ACTION_DOWN)
                    return true;
    

    in the onKey block of code to avoid the event calling twice.

    0 讨论(0)
  • 2020-11-29 21:12

    Try this simple solution:

    In your activity implement onBackPressed

     @Override
        public void onBackPressed() {
           if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                getSupportFragmentManager().popBackStack();
            } else {
                finish();
            }
        }
    

    This will work if you want to pop the top fragment on each back press. Note:- While adding fragment to activity always do add the transaction to back stack for this to work

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