PopBackStack but keep the first fragment in android

前端 未结 10 567
[愿得一人]
[愿得一人] 2020-12-10 12:31

I am working on fragment transaction, and the backstack is like this:

fragA => fragB => fragC => fragD

I would like to return to f

10条回答
  •  醉话见心
    2020-12-10 12:43

    You an override the onbackpressed (Mainactivity) and using the getBackStackEntryCount() of the fragment manager, you can check if its not equal to 1 and popbackstack on only that condition.

    @Override
    public void onBackPressed() {
      if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
         if (!(getSupportFragmentManager().getBackStackEntryCount() == 1)) {
          getSupportFragmentManager().popBackStack() ;
        }
      }    
    }
    
    

提交回复
热议问题