Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentManager - IllegalStateException - No Host

I'm facing very strange issue with this stacktrace:

Fatal Exception: java.lang.IllegalStateException No host
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1194)
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1189)
    android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManager.java:2001)
    android.support.v4.app.Fragment.performActivityCreated (Fragment.java:1976)
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1051)
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1207)
    android.support.v4.app.BackStackRecord.run (BackStackRecord.java:738)
    android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1572)
    android.support.v4.app.FragmentManagerImpl$1.run (FragmentManager.java:493)
    android.os.Handler.handleCallback (Handler.java:733)
    android.os.Handler.dispatchMessage (Handler.java:95)
    android.os.Looper.loop (Looper.java:146)
    android.app.ActivityThread.main (ActivityThread.java:5593)
    java.lang.reflect.Method.invokeNative (Method.java)
    java.lang.reflect.Method.invoke (Method.java:515)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1283)
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1099)
    dalvik.system.NativeStart.main (NativeStart.java)

I tracked 4 occurences of this crash on multiple Android versions (4.1.2, 4.2.2, 6.0). I use o lot of Fragments, which means I can't figure out where exactly exception is thrown.

Code of FragmentManager:

void moveToState(int newState, int transit, int transitStyle, boolean always) {
    if (mHost == null && newState != Fragment.INITIALIZING) {
        throw new IllegalStateException("No host");
    }
    ...
}

In my opinion, this happens when there is no host Activity belonging to Fragment. Ive read this exception is sometimes thrown whensetRetainInstance()` is called, but I don't use it at all.

Thanks for every advice.

like image 904
skywall Avatar asked Oct 19 '15 09:10

skywall


2 Answers

If you have nested Fragment (which have parent Activity extends FragmentActivity), then use getChildFragmentManager() instead of getFragmentManager().

You can also use getSupportFragmentManager().

EDIT: If this Fragment is a child of another Fragment, the FragmentManager returned here will be the parent's getChildFragmentManager().

NOTE: Return the host object of this fragment. May return null if the fragment isn't currently being hosted. See http://developer.android.com/reference/android/app/Fragment.html#getHost()

like image 72
pRaNaY Avatar answered Oct 12 '22 16:10

pRaNaY


I'm not sure why you are getting this error. I got this error when I was reusing Fragments and not calling

    fragmentManager.executePendingTransactions()
like image 27
Krtko Avatar answered Oct 12 '22 17:10

Krtko