nowinandroid
nowinandroid copied to clipboard
[FR]: Misused of DisposableEffect instead of LaunchedEffect
Is there an existing issue for this?
- [x] I have searched the existing issues
Describe the problem
in MainActivity to update the dark content of the system bars to match the theme, there is a DisposableEffect which onDispose method is empty with no comment
DisposableEffect(systemUiController, darkTheme) {
systemUiController.systemBarsDarkContentEnabled = !darkTheme
onDispose {}
}
this code exists in MainActivity:103
Describe the solution
I can update it and use LaunchedEffect instead
Additional context
No response
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
I think DisposableEffect is more suitable. LaunchedEffect is used to call suspend functions other than normal functions
Sent from my 2201117TG using FastHub
@hoc081098 I do agree with you about not using suspend function but we are not disposing anything at all
I think, an empty onDispose{} is not bad
@mhdabbaghy I also think this is the right usage of DisposableEffect based on it's javadoc.
The onDispose method is empty because there is not special code associated to this callback, but it has to be returned from the DisposableEffect call.
This issue should be closed, unless you have additionnal feedback :)
@SimonMarquis It's a bit of a conflict with the official docs 👀
Agreed that this is not an ideal use of DisposableEffect, as it leads to an empty onDispose which is generally to be avoided.
The reason DisposableEffect is used over LaunchedEffect in this particular case is to update the system UI for a new dark mode setting as soon as possible: If the code was in a LaunchedEffect, it would run slightly later due to dispatching than when it runs now when it is placed in a DisposableEffect, and window flags are especially sensitive to timing.