gpt4 book ai didi

Android 回到抽屉导航中的第一个 fragment

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:49 25 4
gpt4 key购买 nike

我有一个在 NavigationDrawer 中有多个 fragment 的应用。

下面是我的代码:

@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() 中的代码时遇到问题。每当我转到其他 fragment 时,都会显示 HomeFragment。有任何想法吗?非常感谢您的帮助。

更新:之前,当我在 MediaFragment 中并按下后退按钮时,它会自动关闭应用程序。它不应该像,但它应该转到 HomeFragment,当它在 HomeFragment 中并且我单击后退按钮时,它会关闭应用程序。

最佳答案

与其在 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();
}

关于Android 回到抽屉导航中的第一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22278788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com