Navigation drawer, handling the back button to go to previous fragments?

后端 未结 6 1663
陌清茗
陌清茗 2020-12-02 14:45

I\'m using the built-in navigation drawer to run my app. I can\'t quite figure out how to handle the back button. When it\'s pressed I want it to load the very first fragmen

6条回答
  •  离开以前
    2020-12-02 14:57

    Just wanted to report my findings even though this question is a little old for anyone else who may have had the same problem with the accepted answer. For me, doing the method suggested in the accepted answer, made the layers overlap, quickly making them unreadable. The code below (adapted from the accepted answer) avoids the overlaying but still adds the screen to the back stack.

    fragmentManager.beginTransaction().replace(R.id.container, fragment).addToBackStack("fragBack").commit();
    

提交回复
热议问题