导航抽屉 - 汉堡菜单工作但后退箭头打开抽屉 [英] Navigation Drawer - hamburger menu working but back arrow opens the drawer

查看:38
本文介绍了导航抽屉 - 汉堡菜单工作但后退箭头打开抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航抽屉并且汉堡菜单显示在所有正确的屏幕中,但是在其他片段上,当我单击后退箭头时,导航抽屉会在我想返回堆栈中时打开上一屏.

I am using a Navigation Drawer and the hamburger menu is showing in all the right screens, but on the other fragments, when I click on the back arrow, the navigation drawer opens when I want to go back in the stack to the previous screen.

如何更改行为,以便在显示箭头时(而不是汉堡菜单)可以捕获用户点击箭头?

How do I change the behavior so that when the arrow is showing, as opposed to the hamburger menu, I can capture the user clicking on the arrow?

我正在使用带有 Fragment 的导航抽屉.在主要活动中,我使用 AppBarConfiguration 设置顶级目的地:

I am using a Navigation Drawer with Fragments. In the main activity I am setting the top level destinations with the AppBarConfiguration:

AppBarConfiguration config = new AppBarConfiguration.Builder(topLevelDestinations)
                .setDrawerLayout(drawerLayout)
                .build();

    NavigationUI.setupActionBarWithNavController(this, navController, config);
    NavigationUI.setupWithNavController(navView, navController);
    
    navView.setNavigationItemSelectedListener(this);

我正在捕捉顶级目的地的所有选择并正确导航:

I am catching all the selections from the top level destinations and navigating appropriately:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    item.setChecked(true);

    drawerLayout.closeDrawers();

    int id = item.getItemId();

    switch (id) {
      ...
    }
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

推荐答案

getFragmentManager().popBackStack() 方法可能就是您正在寻找的方法.以下是为活动放置的示例代码段:

The getFragmentManager().popBackStack() method is probably what you are looking for. Here's a sample snippet to be placed for activity:

@Override
public void onBackPressed(){
    FragmentManager fm = getFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        Log.i("MainActivity", "popping backstack");
        fm.popBackStack();
    } else {
        Log.i("MainActivity", "nothing on backstack, calling super");
        super.onBackPressed();  
    }
}

如果是片段,请尝试像这样调用片段:

In case of fragments, try calling the fragment like this:

getSupportFragmentManager().beginTransaction()
    .add(detailFragment, "detail") // Add this transaction to the back stack
    .addToBackStack(null)
    .commit();

这篇关于导航抽屉 - 汉堡菜单工作但后退箭头打开抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆