Demystifying Android’s FragmentTransaction and solving “Can not perform this action after onSaveInstanceState”

Waseef Akhtar
Degoo
Published in
2 min readJan 2, 2020

--

You’re probably here because you caught an exception in your code looking a lot like this:

While I cannot demistify the exception and show you a bunch of workarounds better than Alex Lockwood here: https://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html, I do know that this exception has been around for a while and devs seem to be confused about solving it since forever.

I myself got stuck for hours last week when solving this and while Alex Lockwood did help me understand the exception, my situation was a bit different:

I was using Google Mobile Ads SDK to show a RewardedVideoAd and when onReward() is called, a DialogFragment is shown to the user. What this means is that when clicked on a VideoAd, the Sender Activity’s state is stored, is paused, and a VideoAd activity is shown to the user, and upon finishing the VideoAd successfully, a DialogFragment is shown in the Sender Activity, but if the activity’s state…

--

--

Waseef Akhtar
Degoo

Android Engineer, Design Lead at Instabridge.