top of page

Do you know the tricky part of DisposableEffect in Jetpack Compose? šŸ¤”




This is a partial answer to the challenge 🧩 I posted three days ago

Jetpack Compose has introduced ā€œside effectsā€ for handling various Android-specific scenarios.


Such side effects include LaunchedEffect, DisposableEffect, rememberCoroutineScope, and others.


The example in the challenge was probably not the best usage of DisposableEffect - because, as one of my LinkedIn followers has mentioned - this could’ve been solved with rememberCoroutineScope.


RememberCoroutineScope() cancels šŸ›‘ the scope when the enclosing Composable leaves the Composition.


The best usage for DisposableEffect is whenever you need to perform an actual ā€œclean-up.ā€ This usually refers to listener removal.


For example, you might have a GPS sensor whose events you listen to in the UI. In such a case, you should put all your clean-up logic in the onDispose part of DisposableEffect.


Another thing that you should remember about DisposableEffect is that it acts similarly to LaunchedEffect. Both have keys šŸ”‘ that they track. Both recompose when that key šŸ”‘ changes.


In our example, we had a DisposableEffect with a Unit for its key. While such an approach might have a use case for one-off events - it wasn’t appropriate in our example. We wanted to show a new UserProfile when userId changes.


I’ve tried to put the whole explanation in the video šŸŽ„.


What do you think of the previous challenge? Was it too much code?


Also, I’m thinking of doing the explanations with examples on YouTube or in longer-form articles - as I will be able to make them faster than the video below. What do you guys think? šŸ‘‡

Ā 
Ā 
Ā 

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

©2025 by Mykola Miroshnychenko

bottom of page