Jetpack Compose: How can I prevent LaunchedEffect from cancelling an ongoing animation when StateFlow updates rapidly?
06:40 31 Oct 2026

I am developing an educational Android application using Kotlin and Jetpack Compose.

My application displays mathematical solutions step by step using animations. When a user taps the Next Step button multiple times quickly, the currently running animation gets cancelled, and some intermediate steps are skipped.

I want every button press to be processed sequentially, regardless of how quickly the user taps.

class StepViewModel : ViewModel() {

    private val _currentStep = MutableStateFlow(0)
    val currentStep = _currentStep.asStateFlow()

    fun nextStep() {
        _currentStep.value++
    }
}
@Composable
fun StepScreen(
    viewModel: StepViewModel = viewModel()
) {
    val currentStep by
        viewModel.currentStep.collectAsStateWithLifecycle()

    val progress = remember {
        Animatable(0f)
    }

    LaunchedEffect(currentStep) {

        Log.d("StepDebug", "START: $currentStep")

        progress.snapTo(0f)

        progress.animateTo(
            targetValue = 1f,
            animationSpec = tween(700)
        )

        Log.d("StepDebug", "COMPLETE: $currentStep")
    }

    Column {
        Text("Current step: $currentStep")

        Button(
            onClick = viewModel::nextStep
        ) {
            Text("Next Step")
        }

        LinearProgressIndicator(
            progress = { progress.value }
        )
    }
}

When tapping slowly:

START: 1
COMPLETE: 1
START: 2
COMPLETE: 2
START: 3
COMPLETE: 3

When tapping rapidly, the current animation can be cancelled when currentStep changes. Since StateFlow is conflated, intermediate values may also be missed by collectors.

The main issue appears to involve the interaction between StateFlow updates and LaunchedEffect cancellation.

Every button press should be queued and processed in order:

REQUEST: 1
REQUEST: 2
REQUEST: 3

START: 1
COMPLETE: 1

START: 2
COMPLETE: 2

START: 3
COMPLETE: 3

No requested animation step should be skipped or interrupted by a subsequent button press.

What is the correct Kotlin and Jetpack Compose approach to queue rapid button presses and guarantee that every animation completes sequentially without being cancelled by subsequent state updates?

I am looking for a reliable solution suitable for a production educational application.

kotlin android-jetpack-compose