I was wondering how to change Appbar Menu in Jetpack Compose.
In the Fragment world to achieve this is to do something like this:
class SampleFragment : Fragment() {
override fun onCreate(context: Context) {
super.onCreate(context)
setHasOptionsMenu(true)
}
// Set the R.menu.sampleMenu in the AppBar
override fun onCreateOptionsMenu(menu: Menu) {
...
menuInflater.inflate(R.menu.sampleMenu, menu)
}
// To handle clicks on the menu
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Check id of menu item and process as follow
}
...
}
I was wondering how to do the exact same thing in Compose. I was thinking of having like:
// Have a top level state which could be set
var menu by remember { mutableStateOf(emptyList<Menu>()) }
....
TopAppBar {
....
menu.forEach {
TextButton(...)
}
}
....
//and then on my content ill set the `menu` to a value
onCommit {
menu = listOf(Menu1, Menu2, Menu3, ...)
}
I was wondering whether this is the right way to do this or whether there is a better way.
I would love to have some inputs and advice thank you very much.