【问题标题】:Back/Up Arrow not working when using navigation drawer使用导航抽屉时后退/向上箭头不起作用
【发布时间】:2020-08-22 21:07:07
【问题描述】:

使用抽屉式导航时,后退或向上按钮不起作用。图标从汉堡包(已设置顶级目的地)更改,但都打开了导航抽屉。我希望后退/上箭头回到堆栈中。

公共类 MainActivity 扩展 AppCompatActivity 实现 NavigationView.OnNavigationItemSelectedListener, DrawerLocker {

public static final String TAG = MainActivity.class.getSimpleName();

private DrawerLayout drawerLayout;
private NavigationView navView;
private Toolbar toolbar;

private NavController navController;
private ActionBarDrawerToggle drawerToggle;


private AppBarConfiguration appBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    //configure drawer layout
    drawerLayout = findViewById(R.id.drawer_layout);
    navView = findViewById(R.id.drawer_navigation_view);

    navController = Navigation.findNavController(this,R.id.nav_host_fragment);

    appBarConfiguration = new AppBarConfiguration.Builder(R.id.scenarioListFragment,
            R.id.tokenListFragment, R.id.settingsFragment,R.id.historyFragment)
            .setDrawerLayout(drawerLayout)
            .build();

    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

    navView.setNavigationItemSelectedListener(this);

    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

    drawerLayout.addDrawerListener(drawerToggle);
    drawerToggle.syncState();

    if (savedInstanceState == null ) {
        navView.setCheckedItem(R.id.drawer_menu_scenario);
        toolbar.setTitle("Scenarios");
    }
}

我看了this video,这看起来有点简单,但它指的是Kotlin,我的代码也不远了。

This post 也遇到了类似的挑战,尽管在 Kotlin 中我尝试添加一个 setNavigationOnClickListener 并在其中放入一个 logd 语句。它从未被解雇,但解决方案本身似乎没有必要。

This post 似乎涵盖了同样的问题,但没有答案。其中一个回复说 setupActionBarWithNavController,我有:

NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

这个函数有很多重载,我已经尝试了很多,但它们似乎导致我丢失了一组顶级目的地(多个片段的汉堡图标)并且使我的导航不正常。

我试图关注this documentation,但它似乎只是让我从原来的位置退了回来。这个电话:

    AppBarConfiguration appBarConfiguration =
            new AppBarConfiguration.Builder(navController.getGraph())
                    .setDrawerLayout(drawerLayout)
                    .build();

似乎替换了topLevelDestinations并使用了navController.getGraph(),但由于我有多个,我一直在引用fragments themselves

我之前就这个问题做过ask,但我很清楚我没有很好地解释它(答案涉及如何弹出而不是为什么后退箭头不起作用)。我会删除它,但人们确实回答了,所以提供的指导是在这种情况下我不应该删除它。如果造成混乱,我很抱歉。

【问题讨论】:

    标签: android navigation-drawer android-architecture-navigation


    【解决方案1】:

    有两个问题:

    1. 根据Add a navigation drawer documentation:

    使用NavigationUI 时,顶部应用栏助手会随着当前目的地的变化自动在抽屉图标和向上图标之间转换。你不需要使用ActionBarDrawerToggle

    因此,您必须删除所有 ActionBarDrawerToggle 代码。

    1. 根据Setting up the Action bar documentation:

    覆盖 onSupportNavigateUp() 以处理向上导航:

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this,
                R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
    

    请注意,如果您有 included an android:label on your destinations 并且正在使用 setupWithNavController,则您的 if (savedInstanceState == null) 也是不必要的。

    【讨论】:

    • 我的应用程序有一个区域,我需要在其中禁用抽屉和后退/向上按钮且不可见。你会如何建议这样做?
    • 这似乎正是根据Listen for navigation events documentation 构建OnDestinationChangedListener 的目的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    相关资源
    最近更新 更多