Created
June 9, 2023 19:14
-
-
Save saqib-github-commits/19bcc1c8eb17c0148d5ac922f42ddfc3 to your computer and use it in GitHub Desktop.
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 rememberLifecycleEvent(lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current): Lifecycle.Event { | |
var lifecycleEvent by remember { mutableStateOf(Lifecycle.Event.ON_ANY) } | |
DisposableEffect(lifecycleOwner) { | |
val lifecycleObserver = LifecycleEventObserver { _, event -> | |
lifecycleEvent = event | |
} | |
lifecycleOwner.lifecycle.addObserver(lifecycleObserver) | |
onDispose { | |
lifecycleOwner.lifecycle.removeObserver(lifecycleObserver) | |
} | |
} | |
return lifecycleEvent | |
} | |
@Composable | |
fun NewsScreen(viewModel: NewsViewModel = NewsViewModel()) { | |
val lifecycleEvent = rememberLifecycleEvent() | |
LaunchedEffect(lifecycleEvent) { | |
if (lifecycleEvent == Lifecycle.Event.ON_RESUME) { | |
viewModel.fetchNews() | |
} | |
} | |
// list of news | |
LazyColumn { | |
// list of news | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment