Last active
April 14, 2022 05:08
RepeatOnLifecycle in Fragment
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
/** | |
* Launches a new coroutine and repeats `block` every time the Fragment's viewLifecycleOwner | |
* is in and out of `minActiveState` lifecycle state. | |
*/ | |
inline fun Fragment.launchAndRepeatWithViewLifecycle( | |
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | |
crossinline block: suspend CoroutineScope.() -> Unit | |
) { | |
viewLifecycleOwner.lifecycleScope.launch { | |
viewLifecycleOwner.lifecycle.repeatOnLifecycle(minActiveState) { | |
block() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/google/iosched/blob/main/mobile/src/main/java/com/google/samples/apps/iosched/util/UiUtils.kt