我的问题是当我按下后退按钮转到上一个片段时,NavigationVIEw仍然会将旧片段显示为所选片段.如果可能,我想将所选选项更新为屏幕上的片段.
例:
我从A开始,从NavigationVIEw中选择B.当前屏幕为B,NavigationVIEw将所选项目显示为B.如果按下后退按钮,我的当前屏幕再次变为A,但NavigationVIEw将B显示为所选项目.
这是我的onNavigationItemSelected方法:
public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation vIEw item clicks here. int ID = item.getItemID(); HomeFragment fragment = null; Class type = null; switch (ID) { case R.ID.nav_home: type = HomeNavigationFragment.class; break; case R.ID.nav_groups: type = GroupsNavigationFragment.class; break; case R.ID.nav_profile: type = ProfileNavigationFragment.class; break; case R.ID.nav_messages: type = MessageNavigationFragment.class; break; } fragment = HomeFragment.newInstance(mUser,type); FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().replace(R.ID.fragment_container,fragment).addToBackStack("fragment" + code++).commit(); DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }
谢谢.
解决方法 我通过像这样重写onBackpressed修复了我的问题:@OverrIDepublic voID onBackpressed() { DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { FragmentManager manager = getSupportFragmentManager(); if(manager.getBackStackEntryCount() > 0) { super.onBackpressed(); HomeFragment currentFragment = (HomeFragment) manager.findFragmentByID(R.ID.fragment_container); if(currentFragment instanceof HomeNavigationFragment){ mNavigationVIEw.getMenu().getItem(0).setChecked(true); } else if(currentFragment instanceof GroupsNavigationFragment){ mNavigationVIEw.getMenu().getItem(2).setChecked(true); } else if(currentFragment instanceof ProfileNavigationFragment){ mNavigationVIEw.getMenu().getItem(1).setChecked(true); } else if(currentFragment instanceof MessageNavigationFragment){ mNavigationVIEw.getMenu().getItem(3).setChecked(true); } } }}总结
以上是内存溢出为你收集整理的android – 按下后退按钮后重新选择正确的NavigationView菜单项全部内容,希望文章能够帮你解决android – 按下后退按钮后重新选择正确的NavigationView菜单项所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)