Android Navigation Component: Start at a different destination than home, programmatically?

前端 未结 2 1406
别跟我提以往
别跟我提以往 2021-02-05 21:24

I\'m trying to implement a multiple navigation controller with multiple back stack BottomNavigationView, as per the github examples. However, the examp

2条回答
  •  不思量自难忘°
    2021-02-05 21:35

    We had a requirement wherein the displayed start screen would depend if the user has logged on, what we did was the following:

    1. Remove app:startDestination on the navigation XML if it exists
    2. On your main activity's XML, remove the following fields inside the tag: app:defaultNavHost="true" and app:navGraph
    3. On our main Activity's onCreate(), we create a NavGraph object :

      NavController navController = Navigation.findNavController(this, R.id.your_nav_hostfragment);    
      NavGraph navGraph = navController.getNavInflater().inflate(R.navigation.your_navigation_xml);
      
    4. Then depending on your requirement, you can set the start destination on the NavGraph using setStartDestination, then apply the NavGraph to the NavController:

          if (condition) {
              navGraph.setStartDestination(R.id.screen1);
          } else {
              navGraph.setStartDestination(R.id.screen2);
          }
          navController.setGraph(navGraph);
      

提交回复
热议问题