Navigation graph with multiple top level destinations

前端 未结 4 1051
醉话见心
醉话见心 2020-12-09 10:40

I am implementing an android app (in Kotlin, but that is not relevant to the Problem) in my free time and I try to use android jetpack and new libraries. I have a single Act

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 10:53

    You don't have to override AppBarConfiguration. Since version alpha7 AppBarConfiguration has a constructor with a set of ids for all top level destinations.

    Set topLevelDestinations = new HashSet<>();
    topLevelDestinations.add(R.id.fragment1);
    topLevelDestinations.add(R.id.fragment2);
    appBarConfiguration = new AppBarConfiguration.Builder(topLevelDestinations)
                                                 .setDrawerLayout(drawerLayout)
                                                 .build();
    NavigationUI.setupActionBarWithNavController(this, 
                                                 this.navController,
                                                 this.appBarConfiguration);
    

    This is not default as the navigation graph has only a single start fragment which should always be the single entry point of the application.

    Editing the default behavior with AppBarConfiguration does not make it behave as before, every top level fragment is placed on the back stack so back button will go to all top level fragments. It is unclear how I can make top level fragments as the first element of the back stack.

提交回复
热议问题