This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun DisposableEffectScreen( | |
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, | |
viewModel: DisposableEffectViewModel | |
) { | |
val logOnResume by rememberUpdatedState(newValue = { viewModel.logViewEvent() }) | |
// 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_RESUME) { | |
logOnResume() | |
} | |
} | |
// Add the observer to the lifecycle | |
lifecycleOwner.lifecycle.addObserver(observer) | |
// When the effect leaves the Composition, remove the observer | |
onDispose { | |
lifecycleOwner.lifecycle.removeObserver(observer) | |
} | |
} | |
// Content of page | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment