How to change start destination of a navigation graph programmatically [Jetpack]

前端 未结 7 1791
醉酒成梦
醉酒成梦 2020-12-08 00:30

Basically, I have the following navigation graph:

I want to change my starting point in navigation graph to fragment 2 right after reaching it

7条回答
  •  一整个雨季
    2020-12-08 00:48

    In MainActivity.kt

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val inflater = navHostFragment.navController.navInflater
        val graph = inflater.inflate(R.navigation.booking_navigation)
    
        if (isTrue){
            graph.startDestination = R.id.DetailsFragment
        }else {
            graph.startDestination = R.id.OtherDetailsFragment
        }
    
        val navController = navHostFragment.navController
        navController.setGraph(graph, intent.extras)
    

    Remove startDestination from nav_graph.xml

    ?xml version="1.0" encoding="utf-8"?>
    
    
    
    
      
      
    
    

提交回复
热议问题