Home » Android » android – Fragment.isAdded returns true after fragment removed from a container

android – Fragment.isAdded returns true after fragment removed from a container

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an activity with below layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/frameLayoutA"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>
    <FrameLayout
        android:id="@+id/frameLayoutB"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>

In onCreate of activity, I load Fragment_A in frameLayoutA and Fragment_B in frameLayoutB.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fmA=(FrameLayout) findViewById(R.id.frameLayoutA);
    fmB=(FrameLayout) findViewById(R.id.frameLayoutB);

    fragA=new FragmentA();
    fragB=new FragmentB();
    fragC=new FragmentC();
    addFragmentsInActivity(R.id.frameLayoutA,fragA);
    addFragmentsInActivity(R.id.frameLayoutB,fragB);
}

public void addFragmentsInActivity(int id, Fragment fragment)
{
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(id, fragment);
    fragmentTransaction.commit();
}

Using a menu operation I want to load Fragment_B in frameLayoutA and Fragment_C in frameLayoutB. The Menu operation is:

    removeFragmentsInActivity(R.id.frameLayoutB,fragB);
    addFragmentsInActivity(R.id.frameLayoutB,fragC);
    if(!fragB.isAdded()){
            Log.e("check", "fragB already removed from frameLayoutB");
        removeFragmentsInActivity(R.id.frameLayoutB,fragB);
        addFragmentsInActivity(R.id.frameLayoutA,fragB);
    }
    else{
        Log.e("check", "fragB already added");
    }    

    public void removeFragmentsInActivity(int id, Fragment fragment)
    {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }

Fragment_B is not displayed in frameLayoutA. frameLayoutA shows Fragment_A. When Menu operation is clicked again the Fragment_B is loaded.

Debugging I found that after fragB.isAdded() returns true after fragB.remove() operation is done. During 2nd menu operation fragB.isAdded() return false and fragB.add() is executed and FragmentB is shown in frameLayoutA.

I understand commit is an async operation. isAdded return true because commit is async and remove operation commit is not done till the time fragB.isAdded() is called. Is it true?

Kindly suggest the possible solution or approach to solve the issue.

Regards,
Vibhor

How to&Answers:

Yes the transaction is committed asynchronously. If you want to make sure all trasactions have finished before executing isAdded, run:

getFragmentManager().executePendingTransactions();

From documentation for executePendingTransactions():

After a FragmentTransaction is committed with
FragmentTransaction.commit(), it is scheduled to be executed
asynchronously on the process’s main thread. If you want to
immediately executing any such pending operations, you can call this
function (only from the main thread) to do so. Note that all callbacks
and other related behavior will be done from within this call, so be
careful about where this is called from.

So your code should look like:

removeFragmentsInActivity(R.id.frameLayoutB,fragB);
addFragmentsInActivity(R.id.frameLayoutB,fragC);
getFragmentManager().executePendingTransactions();
if(!fragB.isAdded()){
        Log.e("check", "fragB already removed from frameLayoutB");
    removeFragmentsInActivity(R.id.frameLayoutA,fragA);
    addFragmentsInActivity(R.id.frameLayoutA,fragB);
}
else{
    Log.e("check", "fragB already added");
}

Note also fixed removing of fragment A.

Answer:

maybe your can encapture commition of FragmentTransaction like this

private void commitFragmentTransaction(final FragmentTransaction ft, boolean allowStateLoss, boolean now) {
    if (ft == null || ft.isEmpty()) {
        return;
    }

    if (allowStateLoss) {
        if (now) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ft.commitNowAllowingStateLoss();
            } else {
                ft.commitAllowingStateLoss();
                mFragmentManager.executePendingTransactions();
            }
        } else {
            ft.commitAllowingStateLoss();
        }
    } else {
        if (now) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ft.commitNow();
            } else {
                ft.commit();
                mFragmentManager.executePendingTransactions();
            }
        } else {
            ft.commit();
        }
    }
}

commitNow() and commitNowAllowingStateLoss() is Added in API level 24

Calling commitNow is preferable to calling commit() followed by executePendingTransactions() as the latter will have the side effect of attempting to commit all currently pending transactions whether that is the desired behavior or not.