Skip to content

Instantly share code, notes, and snippets.

@raxityo
Created November 18, 2021 15:19
Show Gist options
  • Save raxityo/beacbc5f7169f8622a850bbc6c386242 to your computer and use it in GitHub Desktop.
Save raxityo/beacbc5f7169f8622a850bbc6c386242 to your computer and use it in GitHub Desktop.
/**
* Execute a callback on the main scope after given delay.
*
* @param timeMillis delay in milliseconds
* @param callback callback to execute after the delay
*/
fun View.lifecycleAwarePostDelayed(timeMillis: Long, callback: () -> Unit) {
findViewTreeLifecycleOwner()?.lifecycle?.coroutineScope?.launch(Dispatchers.Main) {
delay(timeMillis)
callback()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment