我非常擅长片段化,而且我试图在这里实现某种目标。
考虑这种情况:我有三个片段F1,F2和F3。现在当我从F1转到F2时没有问题。现在的问题是,F2中有一个按钮,当点击时,用F3替换F2。这意味着,F3应该取代这个地方' F2而不在backstack中创建新项目。其背后的目的是,当用户处于F2或F3时按下后退按钮时,它应该重定向回F1。
我想我可以做到这一点,但是当按下后退按钮时会出现各种各样的混乱。再一次,我想要的是,当F2或F3中的任何一个处于活动状态时,后退按钮应该让我回到F1。
我用来从F1到F2 的代码是:
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
我用用F3替换F2 的代码是:
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentThree();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
我用用F2替换F3 的代码是:
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
任何建议和提示都将非常感激。
编辑: 我已在此处上传了我的项目来源:
此致
答案 0 :(得分:0)
好吧,所以这可能不是最好的方法,但现在它解决了我的问题。当我从F2切换到F3并从F3切换到F2时我尝试了这种方法,并且令人惊讶的是它起作用(如后按钮正常运行):
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentThree();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fr);
fragmentTransaction.addToBackStack(null);
fm.popBackStackImmediate();
fragmentTransaction.commit();
另外,请注意我的onBackPressed是这样实现的:
@Override
public void onBackPressed() {
getFragmentManager().executePendingTransactions();
int count = getFragmentManager().getBackStackEntryCount();
System.out.println("getBackStackEntryCount() VALUE : " + count);
if (count == 0) {
System.out.println("onBackPressed() CALLED");
super.onBackPressed();
//additional code
} else {
System.out.println("popBackStack() CALLED");
getFragmentManager().popBackStack();
}
}
答案 1 :(得分:0)
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 1)
getFragmentManager().popBackStackImmediate();
else super.onBackPressed();
}
从活动到F1
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentOne();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fr);
fragmentTransaction.commit();
从F1到F2
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
在其他片段之间
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentThree();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();