Screen Effects
Here’s the improved version of your documentation text:
Modo provides a set of side effects that are similar to the LaunchedEffect
and DisposableEffect
from Jetpack Compose, but they are tied to the screen lifecycle:
LaunchedScreenEffect
- Executes the givenblock: suspend CoroutineScope.() -> Unit
when the screen is created and cancels the coroutine scope when the screen is removed.DisposableScreenEffect
- Executes the giveneffect: DisposableEffectScope.() -> DisposableEffectResult
when the screen is created and calls theDisposableEffectResult.dispose()
of the returnedDisposableEffectResult
when the screen is removed.OnScreenRemoved
- Executes when the screen is removed from the navigation graph. It is similar to theDisposableScreenEffect
.LifecycleScreenEffect
- A shortcut for subscribing to a screen's lifecycle, usingDisposableScreenEffect
under the hood.
You can use these effects when you need to perform an action when the screen is created and then dispose of it when the screen is removed. For example, you can use it to create and close a DI scope.
Effects are called once per
Screen
instance, when the given function enters the composition and this block hasn't been called for thisScreen
instance.The effect is disposed of when the
Screen
is removed from the navigation graph.
Examples of Usage
Analytics
You can use DisposableScreenEffect
to track screen creation and removal events:
DI Scope Integration
You can use OnScreenRemoved
with remember
to create and close a DI scope. The scope of a Screen
can be identified by screenKey
. Here is a sample of how you can use it with Toothpick: