MaterialNavigation causes SideEffects Channel to not execute in LaunchedEffect
See original GitHub issueDescribe the bug
After including the com.google.accompanist:accompanist-navigation-material:0.16.1
library into my module, my side effects which are collected as flow are not executing anymore after I send a SideEffect via a Channel.
To Reproduce
Steps to reproduce the behavior:
- Include dependency for materialNavigation :
com.google.accompanist:accompanist-navigation-material:0.16.1
- You have to collect the sideEffects Channel in a LaunchedEffect in your composable
- Trigger you sideEffect via ex. a button click
- Observe the sideEffect channel is not collected
Expected behavior
SideEffects channel would work even when including the materialNavigation library. I would expect my Toast to appear.
Environment:
- Android OS version: [e.g. Android 10.0]
- Device: [e.g. Emulator Pixel 3a, also same real device]
- Accompanist version: [v0.16.0, v0.16.1]
Additional context
Not sure if this is a dependency problem or what, when I remove this library everything works fine. I am using the materialNavigation for the modal bottom sheet functionality. Here is an example Composable and ViewModel:
@Composable
fun MyScreenScreen() {
val viewModel = hiltViewModel<MyScreenViewModel>()
val viewState = viewModel.viewState.collectAsState().value
val intentChannel = viewModel.intentChannel
val sideEffects = viewModel.sideEffects
MyScreenSideEffects(sideEffects)
MyScreenScreen(viewState, intentChannel)
}
@Composable
private fun MyScreenSideEffects(
sideEffects: Channel<MyScreenSideEffect>
) {
val context = LocalContext.current
LaunchedEffect(true) {
sideEffects.consumeAsFlow().collect { sideEffect ->
when (sideEffect) {
is MyScreenSideEffect.ShowToast -> {
Toast.makeText(context, "Message", Toast.LENGTH_SHORT).show()
}
}
}
}
}
@Composable
private fun MyScreenScreen(
viewState: MyScreenViewState,
intentChannel: Channel<MyScreenIntent> = Channel()
) {
Button(onClick = { intentChannel.trySend(MyScreenIntent.ButtonClicked) }) {
Text(text = "Click me")
}
}
@HiltViewModel
class MyScreenViewModel @Inject constructor(
val dispatcherProvider: DispatcherProvider
) : ViewModel() {
private val _viewState: MutableStateFlow<MyScreenViewState> =
MutableStateFlow(MyScreenViewState())
val viewState = _viewState.asStateFlow()
private val _intentChannel = Channel<MyScreenIntent>(Channel.UNLIMITED)
val intentChannel = _intentChannel
private val _sideEffects = Channel<MyScreenSideEffect>(Channel.UNLIMITED)
val sideEffects = _sideEffects
init {
viewModelScope.launch(dispatcherProvider.main()) {
launch {
_intentChannel.consumeAsFlow().collect { intent ->
when (intent) {
is MyScreenIntent.ButtonClicked -> {
_sideEffects.trySend(MyScreenSideEffect.ShowToast)
}
is MyScreenIntent.BackButtonClicked -> navigateBack()
}
}
}
}
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Side-effects in Compose - Android Developers
LaunchedEffect : run suspend functions in the scope of a composable ... that doesn't emit UI and causes side effects to run when...
Read more >android - LaunchedEffect and SideEffect in Jetpack Compose
In Compose, there is no such thing as invalidating a view. When you keep your when in the same scope as the state...
Read more >Jetpack Compose Side-Effects I — LaunchedEffect
Side effects are undesirable because they can potentially change the state of the app outside the scope of the composable (global state). This ......
Read more >Jetpack Compose Side Effects Made Easy | by Elye - Medium
Let's start with the most common one LaunchedEffect . This side effect will only be launched on the first composition. It will not...
Read more >Jetpack Compose Side Effects - With Examples - Appcircle Blog
Here are the side effects with example usages: LaunchedEffect: Running suspend functions in composable lifecycle scope. LaunchedEffect is one of ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Did you already have a dependency on Navigation Compose 2.4.0-alpha06? Accompanist Navigation 0.16.X will upgrade you to at least that version.
If you were still depending on an older version of Navigation, I’d upgrade that separately and see if you see the issue with just moving to Navigation 2.4.0-alpha06. You’d want to file an issue against Navigation if you do see an issue due to that upgrade. That will at least rule out Navigation Material as the source of your issue.
Hi, I managed to reproduce the issue in a sample project and opened an issue here: https://issuetracker.google.com/u/1/issues/196076691