Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavGraphArhs are visible only in "start" destination #633

Closed
KarolCieslar opened this issue May 7, 2024 · 6 comments
Closed

NavGraphArhs are visible only in "start" destination #633

KarolCieslar opened this issue May 7, 2024 · 6 comments

Comments

@KarolCieslar
Copy link

KarolCieslar commented May 7, 2024

Here is my SettingsGraphArgs:
image

Here is my TempLockScreen (with @destination(SettingsGraph)):
image

Here is my SettingsScreen (with @destination(SettingsGraph) AND with start=true flag):
image

ViewModel of SettingsScreen:
image

I use this code to navigate to Graph
navigator.navigate( SettingsNavGraph(SettingsGraphArgs(userCard = currentCard.owner)) )

My NavGraphs arguments is empty when i try to get it from TempLockScreen but is visible from SettingsScreen.
Is something what i am doing bad?

@raamcosta
Copy link
Owner

Hi!

NavGraph args are a bit tricky with official Compose Navigation as well. You need the back stack entry correspondent to the nav graph to make sure you'll have the args. If you navigate directly to another graph screen that is not the start one, the args will also not be there. That's why Compose Destinations APIs return nullable type in this case.

So, this is expected and how it works with official navigation library as well. You'll need to adapt to it.

@raamcosta
Copy link
Owner

See also here:
https://composedestinations.rafaelcosta.xyz/v2/arguments/navigation-arguments#navgraph-navigation-arguments

NAV GRAPH ARGUMENTS ARE ALWAYS NULLABLE
Navigation arguments of nav graphs are tricky. They will be present if you navigate to the graph itself (for above example, doing navigator.navigate(ProfileNavGraph(id = "some id"))) and they will not be present if you navigate to one of its destinations directly.
This is how official compose navigation works as well, Compose Destinations makes it clear by returning nullable.
If you know in your case you always navigate to the Graph, you can do requireNavGraphArgs instead which will throw an exception instead of returning nullable type.

@KarolCieslar
Copy link
Author

So i have to navigate to NavGraphs every time when i want to retrive NavGraphsArgs?
For example: I have LoginNavGraphs where i have LoginDestination(start), ForgotPasswordDestination, RegisterDestination.
App start and i navigate to LoginNavGraph (i have arguments filled) then I want to redirect user to ForgotPasswordDestination with LoginNavGraph arguments so how i can navigate to this destination with arguments?

Sorry for my english ;/

@KarolCieslar
Copy link
Author

@raamcosta can you tell me how i can do this? #633 (comment)

@raamcosta
Copy link
Owner

You can also get a hold of the NavBackStackEntry correspondent to your NavGraph, and that one will contain all nav graph arguments.

Something like this:

destinationsNavigator.getBackStackEntry(ProfileNavGraph)?.navGraphArgs<ProfileNavGraphArgs>()

@KarolCieslar
Copy link
Author

KarolCieslar commented May 20, 2024

You can also get a hold of the NavBackStackEntry correspondent to your NavGraph, and that one will contain all nav graph arguments.

Something like this:

destinationsNavigator.getBackStackEntry(ProfileNavGraph)?.navGraphArgs<ProfileNavGraphArgs>()

How about get arguments in ViewModel without pass navigator to ViewModel?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants