How to use NavController with a deeplink Uri and a Bundle of arguments

133 Views Asked by At

In short, I'd like to do this:

void nav(NavController navController, Uri deepLink, Bundle args) {
    navController.navigate(deepLink, args);
}

However, androidx.navigation.NavController only has navigate(Uri deepLink) and some variations on navigate(int resId, Bundle args), and no navigate(Uri deepLink, Bundle args) function. Is there a way in which I can accomplish sending a Bundle of arguments when navigating to some unknown Fragment using a Uri deepLink?

E.g. one thing that I could see helping was if there was a way to obtain the resource ID of the Fragment I'm navigating to from the deeplink, so that I could then just call navController.navigate(resID, args).


To provide some background, I'm using a NavController to navigate between Fragments in a single Activity application. The fragments are divided over lots of different modules with lots of different nav_graphs. The deepLinks are constructed programmatically from some strings in a configuration file or sent to the app from our server and have a standard form (something like "{module}://start{name}"). Some of these fragments now need to know some things about where they're called from, while most won't do anything with this information. At the call site, I don't know which is which.

I'm looking for a relatively neat way to essentially get the functionality of navigate(Uri, Bundle). I'm aware of possible ugly workarounds like editing all the nav_graphs to add optional "safeargs" arguments that most of the modules/fragments won't do anything with and then always calling "{module}://start{name}/{arg1}/{arg2}", or putting the argument bundle in some globally accessible place that the fragment could then access in onCreate or something. I would very much like to avoid such hacks.

Thanks for your help!

0

There are 0 best solutions below