Search code examples
androidandroid-studiokotlinandroid-navigationandroid-safe-args

How to use Safeargs with multiple navGraphs


I am creating an app for a school assignment where I need to use 2 activities, which both have a navGraph with multiple fragments.

I know how to pass data between the fragments, but I need to pass data from the userActivity to the mainActivity using safeArgs.

I need to pass the userId from the userFragment to the mainFragment

I haven't been able to find any information on this, could someone help me out?

user navigation

main navigation


Solution

  • You need to pass the args from UserFragment to MainActivity first, then extract the args inside your MainFragment using MainFragmentArgs.fromBundle(requireActivity().intent.extras). For details check out this simple repository I just created that implements your requirements.

    https://github.com/phamtdat/MultipleNavGraphDemo