Separate Back Stack for each tab in BottomNavigationView Android using Fragments

前端 未结 5 2036
梦毁少年i
梦毁少年i 2020-12-13 07:27

I\'m implementing BottomNavigationView for navigation in an Android app. I am using Fragments to set the content for each tab.

I know how to set up one

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 07:30

    This behavior is supported by the new Navigation Architecture Component (https://developer.android.com/topic/libraries/architecture/navigation/).

    Essentially, one can use NavHostFragment, which is a fragment that controls its own back stack:

    Each NavHostFragment has a NavController that defines valid navigation within the navigation host. This includes the navigation graph as well as navigation state such as current location and back stack that will be saved and restored along with the NavHostFragment itself. https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment

    Here is an example: https://github.com/deisold/navigation


    Edit: Turns out Navigation Architecture Component doesn't support seperate back stacks anyway, as pointed out by the commenters. But as @r4jiv007 mentioned, they are working on it and has offered an "official hack" in the meantime: https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample

提交回复
热议问题