Last active
February 10, 2026 16:53
-
-
Save benigumocom/cf4e18f37c2de66ecdf086e91d9d5553 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 AnalyticsScreen(screenName: String) { | |
| var startTime by remember { mutableLongStateOf(0L) } | |
| ScreenLifecycleObserver { event -> | |
| when (event) { | |
| Lifecycle.Event.ON_RESUME -> startTime = System.currentTimeMillis() | |
| Lifecycle.Event.ON_PAUSE -> { | |
| val duration = System.currentTimeMillis() - startTime | |
| Analytics.sendScreenTime(screenName, duration) | |
| } | |
| else -> {} | |
| } | |
| } | |
| } |
This file contains hidden or 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 HomeScreen(viewModel: HomeViewModel) { | |
| ScreenLifecycleObserver { event -> | |
| if (event == Lifecycle.Event.ON_RESUME) { | |
| // Refresh data whenever the screen becomes active | |
| viewModel.refreshUserStatus() | |
| } | |
| } | |
| // ... UI Implementation | |
| } |
This file contains hidden or 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 PlayerScreen(player: MyVideoPlayer) { | |
| ScreenLifecycleObserver { event -> | |
| when (event) { | |
| Lifecycle.Event.ON_PAUSE -> player.pause() | |
| Lifecycle.Event.ON_RESUME -> player.play() | |
| else -> {} | |
| } | |
| } | |
| // ... UI Implementation | |
| } |
This file contains hidden or 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
| /** | |
| * A Composable that monitors lifecycle events for a specific screen. | |
| */ | |
| @Composable | |
| fun ScreenLifecycleObserver( | |
| onEvent: (Lifecycle.Event) -> Unit | |
| ) { | |
| val lifecycleOwner = LocalLifecycleOwner.current | |
| // Use rememberUpdatedState to ensure the latest lambda is called | |
| // without restarting the effect. | |
| val currentOnEvent by rememberUpdatedState(onEvent) | |
| DisposableEffect(lifecycleOwner) { | |
| val observer = LifecycleEventObserver { _, event -> | |
| currentOnEvent(event) | |
| } | |
| // Start observing the lifecycle | |
| lifecycleOwner.lifecycle.addObserver(observer) | |
| onDispose { | |
| // Remove observer when the Composable is disposed (removed from stack) | |
| lifecycleOwner.lifecycle.removeObserver(observer) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mastering Screen Lifecycle in Jetpack Compose Navigation
https://android.benigumo.com/20260211/screen-lifecycle-jetpack-compose/