loloof64
12/16/2021, 10:41 PM@Composable
: GamePage
, which is using a native library. But this native library must be stopped when not needed any more:
@Composable
fun GamePage() {
val myLib = MyLib()
...
myLib.stop()
}
So, is there a way to ?
1. Build a new MyLib instance at each recomposition
2. But also stop it before the next recomposition
3. Can I wait some milliseconds before "commiting" the next recomposition ?Kirill Grouchnikov
12/16/2021, 10:46 PMDisposableEffect
loloof64
12/16/2021, 10:47 PMKirill Grouchnikov
12/16/2021, 10:48 PMremember
your MyLib
with one of the existing wrappers, and then call stop()
on it in DisposableEffect
of this composableloloof64
12/16/2021, 10:50 PMremember
or rememberSaveable
?Lebron James
12/17/2021, 9:20 AM@Composable
fun HomeScreen(
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
onStart: () -> Unit, // Send the 'started' analytics event
onStop: () -> Unit // Send the 'stopped' analytics event
) {
// Safely update the current lambdas when a new one is provided
val currentOnStart by rememberUpdatedState(onStart)
val currentOnStop by rememberUpdatedState(onStop)
// If `lifecycleOwner` changes, dispose and reset the effect
DisposableEffect(lifecycleOwner) {
// Create an observer that triggers our remembered callbacks
// for sending analytics events
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
currentOnStart()
} else if (event == Lifecycle.Event.ON_STOP) {
currentOnStop()
}
}
// Add the observer to the lifecycle
lifecycleOwner.lifecycle.addObserver(observer)
// When the effect leaves the Composition, remove the observer
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
/* Home screen content */
}
This is an official exampleloloof64
12/17/2021, 9:54 AMLebron James
12/17/2021, 10:22 AM