Imagine the usual behavior of a toolbar in Android.
You define a Toolbar
widget in the Activity
, and can access it using onCreateOptionsMenu
and onOptionsItemSelected
inside your fragments.
However, something like this is not possible with normal Jetpack Compose, as there is no way of accessing the Toolbar
that is defined in the Activity
's Scaffold
.
So think of this scenario. You have an Activity
, with the Scaffold
defined in it, and a NavHost
inside that Scaffold
. The NavHost
contains all the sub-pages of your application (Other Composables). The title can be handled view the Navigation Destination Listener, what remains is the Actions of the Toolbar.
How would you change the toolbar actions depending on the current page/composables you're in? And handle clicks on these actions?
P.S : Using a Toolbar in each of the pages is not a solution, as it makes for a bad user experience when switching between animated pages, where the toolbar will disppear and reappear on each page.
You can use the
TopAppBar
at aScaffold
level and use your current destination to customize thetopBar
.Something like:
Otherwise just use separate
TopAppBar
in each screen.