Search code examples
androidnavigationandroid-jetpack-navigation

Is it possible to pass data from fragment to activity using Navigation Component ? If yes then please let me know how


I have a scenario where I want to pass data from Fragment to Activity using Navigation component.


Solution

  • Yes, you can by using SafeArgs.

    Add activity to your navigation graph and add the argument to it.

    Now you can use the below code to launch the Activity passing the necessary value.

    findNavController().navigate(YourFragmentDirections.actionYourFragmentToYourActivity(YourArgument))
    

    More details on SafeArgs here: https://developer.android.com/guide/navigation/navigation-pass-data#Safe-args