我有一个以下布局的活动
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:id="@+id/frameLayoutA"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
android:id="@+id/frameLayoutB"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
在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