谷歌导航抽屉,处理背压以设置正确的检查迭代

时间:2015-11-03 14:29:45

标签: android android-fragments navigation-drawer onbackpressed

目前使用的Google导航抽屉,在用户反印时遇到一些问题。

  1. 当用户反印时,他们没有更新菜单项
  2. 示例当我单击应用程序时。序列是 A> B> C> B> C ,如果I背压我想要 C> B> A 。我该怎么编码呢?
  3. 我的代码

    public class MainActivity extends AppCompatActivity {
    
        private String appTitle;
        private Toolbar toolbar;
        private NavigationView navigationView;
        private DrawerLayout drawerLayout;
        private TextView toolbarTitle;
        private Fragment fragment;
        private FragmentManager fragmentManager;
        private Title title;
        private MenuItem menuItem2;
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Actionbar
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
            toolbarTitle.setTextColor(Color.parseColor("#FFFFFF"));
            setTitle("");
    
            fragmentManager = getSupportFragmentManager();
            fragment= new HomeFragment();
            title = new Title(getApplicationContext());
            fragmentManager.beginTransaction().replace(R.id.content_frame,fragment , title.getStrHome()).commit();
    
            navigationView = (NavigationView) findViewById(R.id.navigation_view);
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem2 = menuItem;
    
                   menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
    
                    if (menuItem.getItemId() == R.id.nav_home) {
                        fragment = new HomeFragment();
                        appTitle = title.getStrHome();
                    }  else if ((menuItem.getItemId() == R.id.nav_direction)) {
                        fragment = new DirectionFragment();
                        appTitle = title.getStrDirection();
                    } else if ((menuItem.getItemId() == R.id.nav_more)) {
                        fragment = new MoreFragment();
                        appTitle = title.getStrMore();
                    } else if((menuItem.getItemId()==R.id.nav_directory)){
                        fragment = new DirectoryFragment();
                        appTitle = title.getStrDirectory();
                    }
                    else {
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }
                    replaceFragment(fragment,appTitle);
    
                    return true;
                }
            });
    
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
            ActionBarDrawerToggle actionBarDrawerToggle =
         new ActionBarDrawerToggle
             (this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                }
    
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                }
            };
    
    
            drawerLayout.setDrawerListener(actionBarDrawerToggle);
            actionBarDrawerToggle.syncState();
    
        }
    
    
    
        public void onBackPressed() {
            if(drawerLayout.isDrawerOpen(GravityCompat.START)){
                drawerLayout.closeDrawer(GravityCompat.START);
            }
            else {
                if (fragmentManager.getBackStackEntryCount() > 0 ){
                    fragmentManager.popBackStack();
                    toolbarTitle.setText(appTitle);
                    menuItem2.setChecked(true);
    
    
                } else {
                    finish();
                }
            }
    
        }
        public void replaceFragment(Fragment fragment, String tag){
            toolbarTitle.setText(tag);
            FragmentTransaction ft =  fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, tag);
            ft.addToBackStack(tag);
    
            ft.commit();
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
    
    
    }
    

2 个答案:

答案 0 :(得分:0)

如Android tasks and back stack官方页面所述,您可以在Intent标志中使用FLAG_ACTIVITY_CLEAR_TOP了解如何完成所需行为。这张照片很好地证明了这一点。

您的常规后退按钮继续:

enter image description here

指定此标志时,会出现类似于您需要的行为

修改

官方文件指出:

FLAG_ACTIVITY_CLEAR_TOP

  

如果正在启动的活动已经在当前任务中运行,那么所有的活动都不是启动该活动的新实例   其上的其他活动被销毁

这意味着,您的不同活动将不会重复,并且您将拥有一个干净的Back Stack。

在您的示例中, A>B>C>B>C 是不可能的。相反,它将是 A>B>C ,因为 B C 已经存在于堆栈中,它们将不会被添加,而是替换为

答案 1 :(得分:0)

研究Android活动启动模式。

here是明确的解释。 看看吧。