Can I ask a question re Jetpack Compose Navigation 3, please?

I am using a `NavDisplay` inside a "global" `Scaffold` with a `TopAppBar`, because that allows me to have an app-wide menu.

But I would also like the `TopAppBar` to display a different title depending on the navigation backstack.

How do I do that?

#BuildInPublic #Android #Kotlin #JetpackCompose #Nav3 #FollowerPower

Before Nav3, I used the excellent Compose Destinations (https://composedestinations.rafaelcosta.xyz/), and with that I solved it by having a "GlobalNavigation" Component and ViewModel, the latter had a method that allowed other Composables to change the title.

I always found that ugly.

Now that I am moving to Nav3, I'd like to do better.

Overview | Compose Destinations

A KSP library that processes annotations and generates code that uses Official Jetpack Compose Navigation under the hood. It hides the complex, non-type-safe and boilerplate code you would have to write otherwise.

@jexner I believe that the recommendation would be to not have that Scaffold() be global. Extract the menu into a separate composable that can be used from the per-screen Scaffold() and TopAppBar(). That gives you more flexibility (e.g., per-screen app bar actions).

Otherwise, perhaps you could have your entry() lambdas inside your NavDisplay() entryProvider update pageTitle, if the entry is the determining factor for the title.

@commonsguy I thought that, too.

But then I have to pass the 'Navigator' into a bunch of separate menu Composables, and that looks ugly to me. Or is there a way to do that in an elegant way?

The entries are partially injected, following the recipe (https://github.com/android/nav3-recipes/tree/main/app/src/main/java/com/example/nav3recipes/modular/hilt), which means I cannot add anything related to pageTitle into those from other modules, correct?!

nav3-recipes/app/src/main/java/com/example/nav3recipes/modular/hilt at main Β· android/nav3-recipes

Implement common use cases with Jetpack Navigation 3 - android/nav3-recipes

GitHub

@jexner If the issue is literally the Navigator, perhaps instead you pass in an onMenuItemClicked lambda slot parameter to those composables, which they invoke as appropriate.

I am guessing that some people will hoist the Navigator into a composition local to avoid passing it around directly; I do not know if that is a good idea or not.

@commonsguy That first method sounds like a good way, cool!

I had wondered about callbacks, but this could be just one, and it could take some `sealed interface NavEvent` items or so.

Hmm... have to try that...

@commonsguy Oh, and thank you for the quick reply, much appreciated!

@jexner Hey! Loved what you’re building.

I’m curating early products on buildinprocess.com and want yours in.