在我的 Activity 布局中,我有以下NavHostFragment:

<fragment
            android:id="@+id/my_nav_host_fragment"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:defaultNavHost="true"
            app:navGraph="@navigation/my_nav_graph" />

我的问题是如何以编程方式设置navGraph?如何在my_nav_host_fragment实例中转换NavHostFragment View 实例?

最佳答案

我找到了解决方案:

//Setup the navGraph for this activity
val myNavHostFragment: NavHostFragment = my_nav_host_fragment as NavHostFragment
val inflater = myNavHostFragment.navController.navInflater
val graph = inflater.inflate(R.navigation.my_nav_graph)
myNavHostFragment.navController.graph = graph

基于:Navigation Architecture Component- Passing argument data to the startDestination

关于android - 以编程方式设置NavGraph,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50898996/

10-09 15:56