这就是我使用导航抽屉显示片段的方式:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment0();
break;
case 1:
fragment = new Fragment1();
break;
case 2:
fragment = new Fragment2();
break;
case 3:
fragment = new Fragment3();
break;
case 4:
fragment = new Fragment4();
break;
case 5:
fragment = new Fragment5();
}
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment)
.addToBackStack(null)
.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(drawerll);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
假设如果用户导航到Fragment 2-> Fragment 5-> Fragment3.
如果用户单击back button
我需要descending order
在每个片段上显示 和name
,当他转到该特定片段时,我将显示片段,fragment
即:
Fragment 3-> Fragment 5-> Fragment2.
这是我尝试过的:
第一种方法:
FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
for(int i = 0; i < count; ++i) {
fm.popBackStackImmediate();
}
第二种方法:
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
我无法让这个工作**(即..我无法将它移回我来自的片段**)并且无法根据后台堆栈显示片段的名称
任何帮助是极大的赞赏。