getting exception IllegalStateException Can not perform this action after onSaveInstanceState
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The IllegalStateException: Can not perform this action after onSaveInstanceState crash happens when you try to commit a fragment transaction after the activity has saved its state. Once onSaveInstanceState() runs, the system has taken a snapshot of the UI. Any fragment commits after that point would be lost on restore, so Android throws this exception to prevent silent data loss.
Why This Happens
The Android lifecycle proceeds in this order when going to background:
After onSaveInstanceState() executes, the framework has serialized the fragment back stack. If you call FragmentTransaction.commit() after this point, the transaction cannot be included in the saved state and the system throws:
Common triggers:
- An async callback (network response, timer, broadcast receiver) arrives after the activity is backgrounded
- Committing fragments in
onActivityResult()before the activity state is restored - Showing a
DialogFragmentfrom a background thread callback
The Problematic Pattern
Fix 1: Use commitAllowingStateLoss()
Replace commit() with commitAllowingStateLoss() when the transaction is not critical to preserve:
This tells Android to proceed even if the state has been saved. The transaction may be lost if the process is killed, but it will not crash. Use this for UI updates that can be reconstructed (like showing cached data).
Fix 2: Check Lifecycle State Before Committing
isStateSaved (added in Support Library 26.0.0) returns true if onSaveInstanceState() has been called.
Fix 3: Use Lifecycle-Aware Components
Use LifecycleOwner to ensure operations only run when the activity is in a valid state:
Or use lifecycleScope with coroutines to automatically cancel when the lifecycle ends:
Fix 4: Handle onActivityResult Correctly
onActivityResult() is called before onStart() in some Android versions, which means the state is not yet restored:
Fix 5: DialogFragment.show() Alternative
DialogFragment.show() calls commit() internally and can crash:
Common Pitfalls
- Using
commit()in async callbacks: Network calls, handlers, and broadcast receivers can fire afteronSaveInstanceState(). Always check the lifecycle state or usecommitAllowingStateLoss(). - Overusing
commitAllowingStateLoss(): This silently drops transactions if the process dies. Only use it for non-critical UI updates that can be reconstructed, not for user-initiated navigation. - Ignoring
onResumeFragments(): Committing inonResume()can still crash becauseonResume()is called before fragments are fully restored. UseonResumeFragments()instead. - Fragment transactions in
onCreate()of a recreated Activity: If the activity is being recreated from saved state, fragments are already restored. Adding them again creates duplicates. ChecksavedInstanceState == nullfirst. - Nested fragments: Child fragment managers have their own state saving lifecycle. A parent being saved also saves children, so committing child transactions after the parent's
onSaveInstanceState()causes the same crash.
Summary
- The crash happens when committing fragment transactions after
onSaveInstanceState()has serialized the UI state - Use
commitAllowingStateLoss()for non-critical updates that can be lost on process death - Check
supportFragmentManager.isStateSavedbefore committing - Use
lifecycleScope.launchWhenStartedwith coroutines for automatic lifecycle awareness - Handle
onActivityResultdata inonResumeFragments(), not directly in the callback - Override
DialogFragment.show()to usecommitAllowingStateLoss()for safe dialog display

