我尝试删除当前片段并替换为第一个片段PemegangPolis
,每次我点击我的应用程序上的新按钮,但我什么都没有,这是我的代码:
case R.id.btn_new:
android.app.FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
if (pemegangPolis.isAdded()) {
fragTrans.remove(pemegangPolis);
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (tertanggungPolis.isAdded()) {
getFragmentManager().beginTransaction().remove(tertanggungPolis);
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (usulanAsuransi.isAdded()) {
getFragmentManager().beginTransaction().remove(usulanAsuransi).commit();
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (detailInvestasi.isAdded()) {
getFragmentManager().beginTransaction().remove(detailInvestasi).commit();
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (detail_Agen.isAdded()) {
getFragmentManager().beginTransaction().remove(detail_Agen).commit();
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else {
if (dokumenPendukung.isAdded()) {
getFragmentManager().beginTransaction().remove(dokumenPendukung).commit();
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}
}
fragTrans.commit();
mDrawerLayout.closeDrawer(SlidingMenu);
break;
我的代码有什么问题吗?或者我错过了什么?
答案 0 :(得分:3)
此代码适用于我:
Fragment fragment = new TypeOfFrag();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
此代码适用于我。尝试使用以下代码调整您的代码: - )。
答案 1 :(得分:0)
有时您在交易中已经调用了提交。这样第二个(replace
)很可能无法正常工作。
此外,要执行事务,您不必首先删除片段。只需replace
即可。
根据您的应用设置,您可能需要将getFragmentManager()
替换为getSupportFragmentManager
case R.id.btn_new:
android.app.FragmentTransaction fragTrans = getFragmentManager().beginTransaction();
if (pemegangPolis.isAdded()) {
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (tertanggungPolis.isAdded()) {
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (usulanAsuransi.isAdded()) {
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (detailInvestasi.isAdded()) {
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else if (detail_Agen.isAdded()) {
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}else {
if (dokumenPendukung.isAdded()) {
fragTrans.replace(R.id.frame_container, new PemegangPolis());
}
}
fragTrans.commit();
mDrawerLayout.closeDrawer(SlidingMenu);
break;
答案 2 :(得分:0)
尝试如下......
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new PemegangPolis());
transaction.addToBackStack(null);
transaction.commit();