Skip to content

Instantly share code, notes, and snippets.

@hongbeomi
Created June 13, 2021 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongbeomi/c0b775a700fd6f3a8dafb0844d19f970 to your computer and use it in GitHub Desktop.
Save hongbeomi/c0b775a700fd6f3a8dafb0844d19f970 to your computer and use it in GitHub Desktop.
@Composable
fun <T> getLifecycleAwareState(
flow: Flow<T>,
initialValue: T,
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED
): State<T> {
return remember(flow, lifecycleOwner) {
flow.flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState)
}.collectAsState(initialValue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment