我是android的新手,我从MianActivity被称为fragment_1,然后我从fragment_1被称为fragment_2。这工作正常。当我从片段_2按回按钮时,它不起作用(我想回到fragment_1)
Fragment someFragment = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.relative_layout_to_replace, someFragment );
transaction.addToBackStack(null);
transaction.commit();
答案 0 :(得分:1)
此代码适用于我
@Override
public void onBackPressed(){
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
finish();
}
else {
super.onBackPressed();
}
}
答案 1 :(得分:0)
您的片段已添加到backstack中。但是你需要“弹出背驮式”一次,让Android设备显示你添加到其后台的片段。 只需在Activity的onBackPressed()
中添加这些代码行 foreach ($content->find('div[class=postbody]') as $element)
答案 2 :(得分:0)
来自主要活动:
getSupportFragmentManager().beginTransaction()
.add(R.id.something,new Fragment1()).addToBackStack(null).commit();
来自fragment1:
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.something,new Fragment2()).addToBackStack(null).commit();
答案 3 :(得分:0)
注意: - 始终在事务中添加第一个片段,然后替换事务中的每个片段,以便返回到先前的片段,您必须将该片段添加到backstack,如下所示
FragmentTransaction fragTrans = fragmentManager.beginTransaction();
fragTrans.addToBackStack("DashboardFragment");
fragTrans.replace(contentFrameId, openDashBoard, "OpenDashboardFragment");
在MainActivity中使用onBackPressed
if (fragmentManager.getBackStackEntryCount() == 1) {
AlertExit();
} else {
int backStackId = fragmentManager.getBackStackEntryAt(i).getId();
fragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);