后段从容器中删除Fragment.isAdded返回true [英] Fragment.isAdded returns true after fragment removed from a container

查看:320
本文介绍了后段从容器中删除Fragment.isAdded返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动有以下布局

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>

在活动的onCreate,我在frameLayoutA和Fragment_B在frameLayoutB加载Fragment_A。

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

使用我想加载Fragment_B在frameLayoutA和Fragment_C在frameLayoutB菜单操作。菜单操作是:

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不显示在frameLayoutA。 frameLayoutA显示Fragment_A。当再次点击菜单操作的Fragment_B被加载。

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

调试我发现,经过fragB.isAdded()返回true后完成fragB.remove()操作。在fragB.isAdded二级菜单操作()返回false和fragB.add()被执行,FragmentB显示在frameLayoutA。

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.

据我所知承诺是一种异步操作。 isAdded返回true,因为承诺是异步和删除操作提交不这样做,直到时间fragB.isAdded()被调用。是真的吗?

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.

问候, Vibhor

Regards, Vibhor

推荐答案

是事务被异步提交。如果你想确保所有trasactions执行之前完成<$​​ C $ C> isAdded ,运行:

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

getFragmentManager().executePendingTransactions();

这是为<一个文件href="http://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions%28%29"><$c$c>executePendingTransactions():

在进行FragmentTransaction致力于与   FragmentTransaction.commit(),它预计要执行   上异步进程的主线程。如果你想   立即执行任何此类未决的操作,你可以调用这个   功能(只从主线程)这样做。请注意,所有的回调   和其他相关的行为将被从该呼叫中完成的,因此是   小心的地方,这是从调用。

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.

所以,你的code应该是这样的:

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

还要注意固定删除片段A的。

Note also fixed removing of fragment A.

这篇关于后段从容器中删除Fragment.isAdded返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆