STAR-ZERO / navigation-keep-fragment-sample

392 stars 63 forks source link

Couldn't pass graph arguments #16

Open mirjalal opened 5 years ago

mirjalal commented 5 years ago

Nice approach to gain the functionality. I want to test this approach for my case, however I didn't succeed. I followed all steps carefully (but it does not mean I did not miss something, of course) to adopt it to app. In my case, I wanted to pass this step:

val data2Pass = 4 // it is guaranteed that this variable CAN NOT be null
navController.graph.addArgument(
    "bundle_name,
    NavArgument.Builder().setDefaultValue(data2Pass).build()
)

When I tried to handle that value in fragment, it throws NPE: val passedData = (findNavController().graph.arguments["bundle_name"]?.defaultValue) as Int // <-- NPE

Do you have any suggestion to handle this issue?

STAR-ZERO commented 5 years ago

Sorry, I'm too late 🙇 I guess, NavGraph instance passing the argument is different from the NavGraph instance getting the argument. In this sample, If you pass arguments with HomeContainerFragment, please try below.

(childFragmentManager.findFragmentById(R.id.nav_host_fragment) as? NavHostFragment)?.navController?.let {  navController ->
    val data2Pass = 4
    navController.graph.addArgument("bundle_name", NavArgument.Builder().setDefaultValue(data2Pass).build())
}