我有一个在NavigationDrawer中有多个片段的应用程序。
以下是我的代码:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_search).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new GalleryFragment();
break;
case 2:
fragment = new ContactFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment).commit();
int position = 0;
fragment = new HomeFragment();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
它基本上是什么,首先是HomeFragment,当我想去另一个fragment
MediaFragment并决定回去按后退按钮时,我想去HomeFragment,当我想要关闭应用程序时再次按下后退按钮,它现在将像gmail应用程序一样关闭应用程序。我怎样才能做到这一点?我使用我放在onBackPressed()
中的代码时遇到了问题。每当我转到其他片段时,HomeFragment就会显示出来。有任何想法吗?非常感谢您的帮助。
更新:在我MediaFragment
之前按下后退按钮之前,它会自动关闭应用。它不应该是,但它应该转到HomeFragment
,当它在HomeFragment
时,我点击后退按钮然后它将关闭应用程序。
答案 0 :(得分:8)
为什么不跟踪选择的最后位置,而不是在onBackPressed中复制你的代码?
int selectedPosition = 0;
private void displayView(int position) {
... // All your current code
selectedPosition = position;
}
@Override
public void onBackPressed() {
if (selectedPosition != 0) {
displayView(0);
} else {
super.onBackPressed();
}
答案 1 :(得分:1)
为什么不在执行交易时简单地将片段添加到backstack,如@Raghunandan所说。见下面的代码,
if (fragment != null) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction()
.replace(R.id.frame_container, fragment).commit(); //Changed from fragmentManager to FragmentTrasaction
fragmentTransaction.addToBackStack(null); //this will add the fragment to backstack and hence pressing back will take to previous trasaction
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
当您退出时,不要添加HomeFragment else,在您退出应用程序之前会看到一个空白页面(或类似的东西)(您必须再次按回来,以避免添加HomeFragment)。将除此之外的所有片段添加到Backstack中,您将能够按照自己的意愿继续操作。此外,当您从片段加载活动时,它会自动由应用程序在Backstack中维护,因此无需担心。
请根据您的要求告诉我。