Jetpack navigation : send parameters to startDestination

A是一个导航图,包含几个Fragment,B是一个子导航图,包含几个Fragment。

1.in A Activity
startActivity(
intentFor(
key to value
)
2.in B Activity
(1)
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
val inflater = navController.navInflater
val graph = inflater.inflate(R.navigation.member_list_navigation)
(2)
val bundle = Bundle()
bundle.putParcelable(key1, fetchArgFromAActivity())
val arg = NavArgument.Builder().setDefaultValue(bundle).build()
graph.addArgument(key2, arg)
(3)
navController.graph = graph
(4)
android:id="@+id/navFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
//app:graph="@navigation/nav_whatever"
app:defaultNavHost="true"
/>

3.in B Fragment
val value = (arguments?.get(key2) as Bundle).getParcelable(key1)

it works, but really bother.

你可能感兴趣的:(Jetpack navigation : send parameters to startDestination)