Fragment has not been attached yet?

I was facing the same issue in my app.

Stack Trace:

java.lang.IllegalStateException: Fragment has not been attached yet. android.support.v4.app.Fragment.instantiateChildFragmentManager(Fragment.java:2154)
android.support.v4.app.Fragment.getChildFragmentManager(Fragment.java:704)

The app was crashing at this line:

function performSomeActionInChildFragments() {
    List<Fragment> fragments = getChildFragmentManager().getFragments();
    ...
}

This happens when the fragment is no longer attached to its parent activity/fragment. So a simple isAdded() check should resolve this issue for you.

Fix:

function performSomeActionInChildFragments() {
    if (!isAdded()) return;
    List<Fragment> fragments = getChildFragmentManager().getFragments();
    ...
}

From documentation:

isAdded() – Return true if the fragment is currently added to its activity.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)