Can not perform this action after onSaveInstanceState 🤖 Android

IllegalStateException: Can not perform this action after onSaveInstanceState (fragment transaction)

A FragmentTransaction (or DialogFragment.show/navigation) ran after the Activity saved its state, typically from an async callback.

Seen on: Android

Meaning

After onSaveInstanceState, committing fragment changes would be lost on restore. Run UI changes only when the lifecycle is at least STARTED (repeatOnLifecycle, launchWhenStarted), or use commitAllowingStateLoss when loss is acceptable.

Common causes

  • Async result arriving while the app is in background
  • Navigation triggered from onActivityResult/onResume edge cases
  • Showing DialogFragment after onStop

⚡ Quick fix

  1. Collect flows with repeatOnLifecycle(STARTED)
  2. Check lifecycle.currentState.isAtLeast(STARTED)
  3. Use commitAllowingStateLoss only if losing the change is fine

Detailed fix by platform

Kotlin

  1. kotlin
    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            viewModel.events.collect { navigate(it) }
        }
    }

How to diagnose

  1. Timing — Was the app backgrounded?
  2. Source — Which callback triggers the transaction?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.