java.lang.IllegalStateException: No instantiated fragment for (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) crash in Android

28.5k views Asked by At

I rarely get a crash in my Android logs with a stack trace like this:

java.lang.IllegalStateException: No instantiated fragment for (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3644)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5782)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5673)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Caused by: java.lang.IllegalStateException: No instantiated fragment for (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
at androidx.fragment.app.FragmentStore.restoreAddedFragments(FragmentStore.java:62)
at androidx.fragment.app.FragmentManager.restoreSaveStateInternal(FragmentManager.java:2602)
at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:2722)
at androidx.fragment.app.FragmentController.attachHost(FragmentController.java:117)
at androidx.fragment.app.FragmentActivity.lambda$init$3$androidx-fragment-app-FragmentActivity(FragmentActivity.java:139)
at androidx.fragment.app.FragmentActivity$$ExternalSyntheticLambda3.onContextAvailable(Unknown Source:2)
at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.kt:84)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:377)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:216)
at com.xxx.xxx.MyActivity.onCreate(MyActivity.java:31)
at android.app.Activity.performCreate(Activity.java:8342)
at android.app.Activity.performCreate(Activity.java:8321)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)

Note I replaced my fragment ID in the logs with all Xs, but it'd be some random UUID, like 8a5ae1d0-23db-489c-94de-5b1f4d641685. What's causing the crash?

1

There are 1 answers

0
georgiecasey On

In my case, this was caused by interference with the saved fragment state of an activity when state was already saved and when one of fragments was a retained fragment. I can't see how it'd happen without retained fragments somewhere but I've seen suggestions online that it can.

Here's how it happened for me: Activity A adds a retained fragment that is running an AsyncTask. I go to Activity B and activity state is saved, including the retained fragment. In the onPostExecute of the AsyncTask that finishes when I'm in Activity B, I have a reference to Activity A and I remove the retained fragment (yes this isn't that common) from the FragmentManager. In Activity B, I rotate the phone and I return to Activity A, this rotation triggers a config change, it tries to recreate from saved state which references the removed retained fragment and I get the 'No instantiated fragment' crash.

This can't happen if fragments aren't retained as Android just recreates fragments on activity rotation anyway. And there's code in the fragment library that doesn't remove retained fragments from mActive when state is saved (so onSaveInstanceState called), presumably to prevent rare crashes when code is running while phone is being rotated. Basically I don't think Android expects devs to be adding/removing fragments from an activity when it's in the background.

This is a self-answered question as I found very little online when researching this error. My crash was in androidx fragment library 1.6.0. My fix was to only remove the retained fragment when Activity A is in at least a RESUMED state. It works for my case but if you're Googling this because you're seeing it in your Play Console logs, you might be getting it for different reasons. Comment below with details.