Navigation Architecture Component- Passing argument data to the startDestination

android navigation component arguments
android navigation arguments
required argument is missing and does not have an android:defaultvalue
android navigation pass object
safe args android
navigation component android
android navigation component example
pass data back to previous fragment using android navigation

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 that intent data to the startDestination fragment as argument how to do that?

TLDR: You have to manually inflate the graph, add the keys/values to the defaultArgs, and set the graph on the navController.

Step 1

The documentation tells you to set the graph in the <fragment> tag in your Activity's layout. Something like:

<fragment
    android:id="@+id/navFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:graph="@navigation/nav_whatever"
    app:defaultNavHost="true"
    />

REMOVE the line setting the graph=.

Step 2

In the Activity that will be displaying your NavHostFragment, inflate the graph like so:

val navHostFragment = navFragment as NavHostFragment
val inflater = navHostFragment.navController.navInflater
val graph = inflater.inflate(R.navigation.nav_whatever)

Where navFragment is the id you gave your fragment in XML, as above.

Step 3 [Crucial!]

Create a bundle to hold the arguments you want to pass to your startDestination fragment and add it to the graph's default arguments:

val bundle = Bundle()
// ...add keys and values
graph.addDefaultArguments(bundle)

Step 4

Set the graph on the host's navController:

navHostFragment.navController.graph = graph

Pass data between destinations, <argument android:name="username" app:type="string" /> </fragment> </​navigation>. Pass data from the source fragment by getting the nav  Create a bundle to hold the arguments you want to pass to your startDestination fragment and add it to the graph’s default arguments: val bundle = Bundle // add keys and values graph. addDefaultArguments (bundle) Step 4. Set the graph on the host’s navController: navHostFragment. navController. graph = graph

It had been fixed in 1.0.0-alpha07. See detail.

The solution is similar to Elliot Schrock's answer, but wrapping by official API.

We have to manually inflate NavHostFragment or graph

Use

NavHostFragment.create(R.navigation.graph, args)

Or

navController.setGraph(R.navigation.graph, args)

The args are the data we want to pass to start destination.

Passing data in android navigation architecture component part-2, Activity B host a navigation graph from the new Navigation Architecture Component.I want to pass that intent data to the startDestination fragment as argument  Passing argument(s) to a nested Navigation architecture component graph (2 answers) Closed 9 months ago . Let's assume I have graph1.xml which contains FragmentA and FragmentB.

OK, I found a solution to that problem thanks to Ian Lake from the Google team. Let say you have an activity A that will start activity B with some intent data and you want to get that data in the startDestination you have two options here if you using safe args which is my case you could do

StartFragmentArgs.fromBundle(requireActivity().intent?.extras)

to read the args from the Intent. If you don't use safe args you can extract the data from the bundle your self-using requireActivity().intent?.extras which will return a Bundle you can use instead of the fragment getArguments() method. That's it I try it and everything works fine.

An Android Navigation Component Tutorial, An Overview of the Android Jetpack Navigation Architecture Component, Creating and of the Android Studio navigation graph editor, the example project will also implement the passing of data between origin and destination screens using type-safe arguments. app:startDestination="@id/mainFragment"​> <fragment  Pass data from the source fragment by getting the nav controller of view as pass data from source fragment To use the bundleOf() is the extension method of androidx.

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<ProductDetailsArgs>()  
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.

Android Navigation Architecture Component: How to pass bundle , If you want to pass some data to the start destination directly as arguments from host activity, you need to manually set your host's navigation graph inside the  How does one pass argument(s) to a nested Navigation architecture component graph? Let's say I construct my navigation graph to navigate from FragmentA --> Nested, where Nested contains Fragmen

So finally, I have found a solution to this issue.

At the time of writing this answer, I am using Navigation 2.2.0-alpha01

If you want to pass some data to the start destination directly as arguments from host activity, you need to manually set your host’s navigation graph inside the host activity’s onCreate() method, as shown below:

Get you navController:

val navController by lazy { findNavController(R.id.<your_nav_host_id>) }

Then in the host activity's onCreate()

val bundle = Bundle()
bundle.putString("some_argument", "some_value")
navController.setGraph(R.navigation.<you_nav_graph_xml>, bundle)

Or if you want to pass the whole intent extras as it is to the startDestination:

navController.setGraph(R.navigation.<you_nav_graph_xml>, intent.extras)

Since intent.extras would return a Bundle only

When you are setting the navGraph using setGraph() method, you should avoid setting the app:NavGraph attribute in the NavHostFragment definition, because doing so results in inflating and setting the navigation graph twice.

While reading these arguments in your startDestination fragment:

If you are using the Safe Args Plugin (which is very much recommended), then in your fragment:

private val args by navArgs<DummyFragmentArgs>()

Safe Args plugin would generate an Args class by appending Args to your fragment name. For example, if you fragment is called DummyFragment then Safe Args would generate a class called DummyFragmentArgs

where navArgs<> is an extension function defined in Android KTX

If you are not using Android KTX, you can get the args object like:

val args = DummyFragmentArgs.fromBundle(arguments!!)

Once you've acquired the arguments object, you can simply fetch your arguments:

args.someArgument

Notice how we passed "some_argument" as argument, and we are reading it as someArgument using Safe Args

If you are not using Safe Args (there is no reason to not use it though), you can access your arguments like this:

arguments?.getString("some_argument")

All of this is documented in Migrate to Navigation Component documentation here: https://developer.android.com/guide/navigation/navigation-migrate#pass_activity_destination_args_to_a_start_destination_fragment

Android navigation pass data, I want to pass that intent data to the startDestination fragment as argument how to do Sep 09, 2018 · I really like the new Navigation Architecture Component. Navigating between screens — passing data, handling the back stack, implementing deep links, etc. — can be complicated. It also comes with quite a bit of boilerplate. To top it off, you should adhere to the new principles of navigation: The app should have a fixed start destination. A stack is used to represent the navigation state of an app.

WTF, Navigation component is a part of Android Jetpack Library. Every app should have a fixed start destination. A stack like structure saves the navigation state of the app. Up and Up and Back actions, type safety while passing data and much more. Click on the + button to add argument you want to pass. One of the core parts of the Navigation component is the navigation host. The navigation host is an empty container where destinations are swapped in and out as a user navigates through your app. The navigation host is an empty container where destinations are swapped in and out as a user navigates through your app.

Android Jetpack: Navigation to the rescue, JetPack's navigation component was built with the purpose of bringing solutions to these issues. Passing data between Fragments can get tedious and is error-​prone, Ideally, we'd like to pass and receive arguments in an easy manner Issues such as how to structure these deep links, how to pass the  I really like the new Navigation Architecture Component. While still in alpha, the library is already mature enough to start using in Production. Google is doing a great job seeking ideas and bug reports from developers and helping out on StackOverflow (just use the right tags: android-architecture-navigation , android-navigation-component ).

The Navigation Architecture Component Tutorial: Getting Started , Navigating between screens — passing data, handling the back stack, implementing deep links, The app should have a fixed start destination. To use the Navigation architecture component, you must use Android Studio 3.2 or higher. You'll learn how to pass arguments between destinations shortly. The Navigation component is a library that can manage complex navigation, transition animation, deep linking, and compile-time checked argument passing between the screens in your app. This document serves as a general-purpose guide to migrate an existing app to use the Navigation component.

Comments
  • I'm pretty sure the navigation editor has options for "arguments" and then you can add the safe-args gradle plugin but the details are a bit hazy for me atm
  • Yes i know but there is no way to pass arguments to the start destination fragment
  • Your solution will work but quite complex i found a better way i will but in the other answer
  • This solution is not working anymore in 1.0.0-alpha09. The addDefaultArgument function has been removed.
  • addDefaultArgument has been removed from Navigation Architecture Component. Please have a look at my answer below to see the official recommended approach.
  • Where does StartFragmentArgs class comes from? Is it included in the navigation arch dependency?
  • It's a generated class by the navigation. If you are using safe argument. For example the fragment here called StartFragment so the generated arg class will be StartFragmentArgs. If the fragment called FooFragment the generated class will be FooFragmentArgs and so on
  • I tried using it and added this line in onCreate of Activity StartFragmentArgs.fromBundle(requireActivity().intent?.extras) but argument's sent to my startDestination are null
  • When you start the activity that has the graph you are passing the data as intent and extract it in requireActivity().intent?.extras
  • You need to use navigation safe args Gradle plugin
  • Hi @Johan Paul Can you please tell me what is R.id.main_content in this?
  • @rupesh It is the ID for the NavHostFragment where you would normally specify the navigation graph in the XML layout file.