compose-destinations icon indicating copy to clipboard operation
compose-destinations copied to clipboard

Destination within multiple NavGraphs is only added to the first one

Open AhmedMourad0 opened this issue 2 years ago • 9 comments

Hello there! hope you're doing well,


@NavGraph
annotation class NavGraph1(val start: Boolean = false)

@NavGraph1(start = true)
@Destination(/*...*/)
@Composable
fun NavGraph1StartScreen(navigator: DestinationsNavigator) {
    //...
}

@NavGraph
annotation class NavGraph2(val start: Boolean = false)

@NavGraph2(start = true)
@Destination(/*...*/)
@Composable
fun NavGraph2StartScreen(navigator: DestinationsNavigator) {
    //...
}

@NavGraph1
@NavGraph2
@Destination(/*...*/)
@Composable
fun MyScreen(navigator: DestinationsNavigator) {
    //...
}

MyScreenDestination is only being added to the destinations of NavGraph1

AhmedMourad0 avatar Jun 20 '22 20:06 AhmedMourad0

At the moment this is expected. Each Destination can only have a single NavGraph. If you want that behaviour you need to make two Composables one for each NavGraph and both call a common composable. This forces you to give different names, which creates two different generated Destinations, which in turn makes you be able to navigate to specific one you want 🙂

raamcosta avatar Jun 20 '22 21:06 raamcosta

I’ll leave it open but it’s probably low priority enhancement at best 🙂

raamcosta avatar Jun 20 '22 21:06 raamcosta

I don't even know if this would be possible but is there a way to check if a destination is used multiple times? If so it should be easy to generate a wrapper composable with the same name plus adding a numeric value at the end that contains that composable.

It's not perfect and could eventually be optimized to specify more details but would allow this action without having to manually create said files.

Vahalaru avatar Jul 07 '22 04:07 Vahalaru

I think this is actually a very useful feature, it'll enable navigating to the same destination from multiple graphs without any workarounds, right? or i'm missing something?

yallam08 avatar Nov 25 '22 00:11 yallam08

Hi @yallam08!

You can already do that 🤔 Unless with graphs you mean root level graphs aka the ones passed in to DestinationsNavHost calls? In that case, no this doesn’t help to do it more easily than we can also already do it.

This feature would literally create one destination for each graph and all would call your composable, that’s it.

You can achieve the same thing by defining multiple destinations which just calls a common composable. Each assigned to its own graph.

raamcosta avatar Nov 25 '22 00:11 raamcosta

@raamcosta

yes, with graphs i mean the root level. i'm currently using a serparate graph for a flow within my app. e.g, @SignInSignUpNavGraph , @HomeNavGraph , @PaymentNavGraph , etc.. and in many cases i have a screen that's accessed from 3 graphs or more.

now i'll need to define 3 wrapper destinations for the same screen. i'd prefer if the library does this for me and use the same destination. what do you think?

thanks for the great lib btw

yallam08 avatar Nov 25 '22 14:11 yallam08

Just came here to say that I just tried to do this exact thing and was looking to open a new question about it, so essentially a +1.

Just adding my scenario to it:

I have two separate bottom navigation layouts that is the main navigation, but looks different for different users. Some tabs are shared between the bottom navigations, and I would have liked to just share those destinations. I can't put them to the same NavGraph because they have different starting tabs, among other things.

I think it would be a nice addition, but I understand that it might not feel that high prio :)

I had thought it would just add the same destination to two different NavGraphs, but I guess this could mess up things in ways I'm not familiar with 😅

maggud avatar Feb 02 '23 09:02 maggud

would be nice if this is implemented

lolafhkahfkjsghd avatar May 12 '23 09:05 lolafhkahfkjsghd

Also +1 although we are currently going with multiple destinations calling common composable and it is totally fine

lmiskovic avatar Nov 16 '23 10:11 lmiskovic

Implemented for v2! Closing this one.

raamcosta avatar Mar 12 '24 01:03 raamcosta