How can I change FAB action(onClick{}) depending on current screen with Scaffold.
Scaffold(
floatingActionButton = {
FloatingActionButton(onClick = {
//Different actions here depending on current screen
}) { }
}
) {
NavHost(
navController = navController,
startDestination = "route1"
) {
composable(
route = "route1"
) {
ScreenOne()
}
composable(
route = "route2"
) {
ScreenTwo()
}
}
}
For example with ScreenOne() I want to add some entities to database and with ScreenTwo() I'd like to save edited entity. And all this using same FAB shared by Scaffold.
UPDATE: Just understood that I also need to mention that I'm also need FAB to know some data to operate. For example I'd like to save some entity to database by clicking on FAB, so FAB need to know about this entity.
Example: I'm clicking on some item in the list. It opens some navigation direction with some entity. I'm modifying this entity and clicking on FAB to save edited entity.
If you are using
Navigation
, you can retrieve thenavController
byrememberNavController()
and then use this togetCurrentBackStackEntry().destination
, applying conditionals in theonClick
block and setting the action accordingly