Android : Return to previous fragment on back press

前端 未结 5 1355
南笙
南笙 2021-02-12 17:44

I have implemented Navigation Drawer which is a subclass of Activity. I have many fragments in my application. My question goes here

Imagine there are 3 fragments :

5条回答
  •  别那么骄傲
    2021-02-12 18:25

    Use this code on tab change in your main activity to clear the stack.

    int count = getFragmentManager().getBackStackEntryCount();
            if(count>0){
                for (int i = 0; i 

    Then on Back pressed of your main activity do this

     int count = getFragmentManager().getBackStackEntryCount();
    
         if (count == 0) {
             super.onbackpressed();
            }
    else {
            getFragmentManager().popBackStack();
        }
     }
    

提交回复
热议问题