我正在使用一个主要活动和多个片段创建一个Android应用。我正在使用导航抽屉里面有两个项目 - 片段A和片段B.从片段A你可以去2个低级片段和片段B相同。在我的例子中,片段B.1中有一个按钮,片段B.2允许您直接进入片段A.3。 (见下图)
这一切都适用于后端堆栈,但我想实现主页上升按钮。通常,如果您使用多个活动,则可以使用NavUtils类,但我不能将其用于片段。相反,我已经实现了找到here的解决方案。不幸的是,在我的情况下,这并没有正确地遵循导航模式,因为当我点击片段B.2中的按钮转到片段A.3然后单击向上按钮它将我带回片段B.2而不是向上片段A.2。
我所说的导航模式如下所示: i.stack.imgur.com/EsoTl.png
我的应用导航如下所示: i.stack.imgur.com/8WjBr.png
代码:
private FragmentManager.OnBackStackChangedListener
mOnBackStackChangedListener = new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
setActionBarArrowDependingOnFragmentsBackStack();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the title of the current page
title = drawerTitle = getTitle();
// load slide menu item titles
navMenuTitles = getResources().getStringArray(R.array.nav_menu_items);
navMenuLayout = (DrawerLayout) findViewById(R.id.nav_menu_layout);
navMenuList = (ListView) findViewById(R.id.nav_menu_list);
navMenuItems = new ArrayList<NavMenuItem>();
// Instantiate a NavMenuItem object and add it to an ArrayList.
for (int i = 0; i < navMenuTitles.length; i++){
navMenuItems.add(new NavMenuItem(navMenuTitles[i]));
}
// setting the nav menu list adapter
adapter = new NavMenuListAdapter(getApplicationContext(),navMenuItems);
navMenuList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// setup the onItemClickListener to load correct page when user clicks on navigation item
navMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayFragment(position);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this, navMenuLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
getActionBar().setTitle(title);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(drawerTitle);
}
};
navMenuLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
// if this is the first time the app is being opened then open the first page from the list.
if (savedInstanceState == null) {
// on first time display view for first nav item
displayFragment(0);
}
}
@Override
protected void onDestroy() {
getSupportFragmentManager().removeOnBackStackChangedListener(mOnBackStackChangedListener);
super.onDestroy();
}
private void setActionBarArrowDependingOnFragmentsBackStack() {
int backStackEntryCount =
getSupportFragmentManager().getBackStackEntryCount();
mDrawerToggle.setDrawerIndicatorEnabled(backStackEntryCount == 0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.isDrawerIndicatorEnabled() &&
mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else if (item.getItemId() == android.R.id.home &&
getSupportFragmentManager().popBackStackImmediate()) {
return true;
} else {
switch (item.getItemId()) {
// handle other items in actionbar
default:
return super.onOptionsItemSelected(item);
}
}
}