Search code examples
androidandroid-jetpack-composeandroid-jetpack-navigation

How to communicate between Composables in Jetpack Compose


I have MainScreen(). On click on MainScreen() button ModalBottomSheet1() is opened. On click on ModalBottomSheet1() button ModalBottomSheet2() is opened (over first one). Shortly: MainScreen() -> ModalBottomSheet1() ModalBottomSheet2(). I did it, without using Compose Navigation.

Then on click on ModalBottomSheet2() both 1 and 2 bottom sheets should be hidden, exposing MainScreen() to allow clicking on it. After clicking on MainScreen() data should be passed from it to ModalBottomSheet2(). ModalBottomSheet2() should pass data to ModalBottomSheet2() and close itself afterwards.

Full flow: MainScreen() -> ModalBottomSheet1() -> ModalBottomSheet2() -> I am stuck there -> MainScreen() -> ModalBottomSheet2() -> ModalBottomSheet1()

I have idea how to do it with callbacks and var hideBottomSheet by remember { mutableStateOf(false) }, but it seems very complicated. Is there a better way?


Solution

  • Instead of managing visibility with individual states for each modal, use a single state to track the current visible bottom sheet or screen.