Skip to content

Instantly share code, notes, and snippets.

@vitoksmile
vitoksmile / LifecycleEventCoroutineEffect.kt
Created March 3, 2025 16:10
Mastering delays in Android
@Composable
fun LifecycleEventCoroutineEffect(
event: Lifecycle.Event,
coroutineScope: CoroutineScope = rememberCoroutineScope(),
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
onEvent: suspend CoroutineScope.() -> Unit,
) {
if (event == Lifecycle.Event.ON_DESTROY) {
throw IllegalArgumentException(
"LifecycleEventEffect cannot be used to " +
@vitoksmile
vitoksmile / TestComponent.kt
Created March 3, 2025 16:09
Mastering delays in Android
@Composable
fun TestComponent() {
val context = LocalContext.current
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
delay(1.seconds)
Toast.makeText(context, "Hello, Medium", Toast.LENGTH_LONG).show()
}
}
@vitoksmile
vitoksmile / TestFragment.kt
Created March 3, 2025 16:09
Mastering delays in Android
class TestFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) {
delay(1.seconds)
Toast.makeText(requireContext(), "Hello, Medium", Toast.LENGTH_LONG).show()
}
@vitoksmile
vitoksmile / TestFragment.kt
Created March 3, 2025 16:08
Mastering delays in Android
class TestFragment : Fragment() {
override fun onResume() {
super.onResume()
requireView().postDelayed(1.seconds) {
Toast.makeText(requireContext(), "Hello, Medium", Toast.LENGTH_LONG).show()
}
}
}
@vitoksmile
vitoksmile / ViewUtils.kt
Created March 3, 2025 16:08
Mastering delays in Android
fun View.postDelayed(timeout: Duration, action: Runnable) {
postDelayed(action, timeout.inWholeMilliseconds)
doOnDetach { removeCallbacks(action) }
}
@vitoksmile
vitoksmile / TestFragment.kt
Created March 3, 2025 16:07
Mastering delays in Android
class TestFragment : Fragment() {
override fun onResume() {
super.onResume()
requireView().postDelayed({
Toast.makeText(requireContext(), "Hello, Medium", Toast.LENGTH_LONG).show()
}, 1_000)
}
}
@vitoksmile
vitoksmile / TestClass.kt
Created March 3, 2025 16:07
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()
@vitoksmile
vitoksmile / TestClass.kt
Created March 3, 2025 16:06
Mastering delays in Android
class TestClass(
private val context: Context,
) {
fun onResume() {
Handler(Looper.getMainLooper()).postDelayed({
Toast.makeText(context, "Hello, Medium", Toast.LENGTH_LONG).show()
}, 1_000)
}
}
@vitoksmile
vitoksmile / TestClass.kt
Created March 3, 2025 16:06
Mastering delays in Android
class TestClass(
private val context: Context,
) {
fun onResume() {
thread {
Thread.sleep(1_000)
Toast.makeText(context, "Hello, Medium", Toast.LENGTH_LONG).show()
}
}
@vitoksmile
vitoksmile / App.kt
Created February 11, 2025 18:10
ComposeHints
val coroutineScope = rememberCoroutineScope()
val hintController = rememberHintController()
// Now we can dismiss all pending hints from a hint itself
val topAppBarHint = rememberHintContainer {
OutlinedButton(
onClick = {
hintController.dismiss()
}
) { Text("Hint for TopAppBar") }