Android : Return to previous fragment on back press

前端 未结 5 1358
南笙
南笙 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:27

    You have to implement your own backstack implementation as explained here

    Separate Back Stack for each tab in Android using Fragments

    You can call the popFragments() whenever you click the back button in a fragment and call pushFragments() whenever you navigate from one Fragment to other.

    in Short,

    public void onBackPressed()
    {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.popBackStack();
    }
    

提交回复
热议问题