Skip to content

Instantly share code, notes, and snippets.

@vitoksmile
Created March 3, 2025 16:07
Show Gist options
  • Save vitoksmile/b7ee4669c09a70a11d22e0f63054b7cb to your computer and use it in GitHub Desktop.
Save vitoksmile/b7ee4669c09a70a11d22e0f63054b7cb to your computer and use it in GitHub Desktop.
Mastering delays in Android
class TestClass(
private val context: Context,
) {
private val handler = Handler(Looper.getMainLooper())
private var runnable: Runnable? = null
fun onResume() {
runnable = Runnable {
Toast.makeText(context, "Hello, Medium", Toast.LENGTH_LONG).show()
}.also { handler.postDelayed(it, 1_000) }
}
fun onPause() {
runnable?.let { handler.removeCallbacks(it) }
runnable = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment