android – Fragment.isAdded在从容器中删除片段后返回true

前端之家收集整理的这篇文章主要介绍了android – Fragment.isAdded在从容器中删除片段后返回true前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个以下布局的活动
<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>

在onCreate的活动中,我将frameLayoutA中的Fragment_A和Fragment_B加载到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();
}

使用菜单操作,我想在FrameLayoutB中的frameLayoutA和Fragment_C中加载Fragment_B.菜单操作是:

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不显示在frameLayoutA中. frameLayoutA显示Fragment_A.再次单击菜单操作时,将加载Fragment_B.

调试我发现在fragB.isAdded()返回true之后,在fragB.remove()操作完成之后.在第二个菜单操作期间,执行fragB.isAdded()返回false,并执行fragB.add(),并将FragmentB显示在frameLayoutA中.

我明白commit是一个异步操作. isAdded返回true,因为commit是异步的,删除操作commit不会被完成,直到调用fragB.isAdded()为止.是真的吗

请提出解决问题的方法或办法.

问候,
Vibhor

解决方法

是的,事务是异步提交的.如果要在执行isAdded之前确保所有trasactions已经完成,请运行:
getFragmentManager().executePendingTransactions();

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.

所以你的代码应该是:

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");
}

也注意到固定的片段A的去除.

原文链接:https://www.f2er.com/android/312483.html

猜你在找的Android相关文章