Navigation Component: pass safeArgs from activity to a fragment

前端 未结 2 1698
感动是毒
感动是毒 2021-01-04 12:52

I have two graphs, so the first graph move from one fragment to an activity passing safeArgs to the activity.

val action = MyFragmentDirections.actionMyActiv         


        
相关标签:
2条回答
  • 2021-01-04 13:04

    I don't know if this is recommended, but it is working:

    val args = MyActivity.fromBundle(intent.extras)
    navController.navigate(R.id.myActivityFragment, args.toBundle())
    
    0 讨论(0)
  • 2021-01-04 13:22

    Navigation 1.0.0-alpha07 fixed the feature request for passing arguments to the start destination of a graph.

    To use this, you'd need to:

    1. Remove the app:navGraph attribute from your NavHostFragment
    2. Call findNavController(R.id.your_nav_host_fragment).setGraph(R.navigation.your_graph, intent.extras)

    Using the R.id of your NavHostFragment and R.navigation that you previously had on your app:navGraph tag. By passing the arguments into the setGraph call, your starting destination will get the arguments directly, without calling navigate again (which would, by default, create a new instance of the destination on your back stack - not what you want).

    0 讨论(0)
提交回复
热议问题