Navigation Architecture Component- Passing argument data to the startDestination

前端 未结 9 780
情话喂你
情话喂你 2020-12-05 16:43

I have an activity A that start activity B passing to it some intent data. Activity B host a navigation graph from the new Navigation Architecture Component.I want to pass t

9条回答
  •  -上瘾入骨i
    2020-12-05 17:39

    I think this has changed again with the 1.0.0 release. And Google has hidden this information very well in the official documentation. Or at least I struggled to find it, but stumbled upon it in the Migrate to the Navigation component guide. How to pass arguments to the start destination is mentioned here:

    https://developer.android.com/guide/navigation/navigation-migrate#pass_activity_destination_args_to_a_start_destination_fragment

    In short

    1. You have to set the navigation graph programatically:
    findNavController(R.id.main_content)
                    .setGraph(R.navigation.product_detail_graph, intent.extras)
    
    1. Don't set the graph in the NavHostFragment XML declaration.
    2. Read the extras from the receiver side:
    val args by navArgs()  
    val productId = args.productId
    

    Update: Google has said that the official documentation for passing arguments to the initial navigation target is indeed missing. Hopefully this is added soon as part of the Navigation component documentation.

提交回复
热议问题