使用导航组件处理导航-导航抽屉

时间:2020-08-09 09:36:15

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

我使用导航组件来制作表单,因此当用户输入表单时..我想处理导航以使其禁用,或者在按下向上按钮时向用户显示确认消息 我尝试了这些解决方案,但对我没有用here here here here

如果在主活动中覆盖 onSupportNavigateUp()或没有,

onOptionsItemSelected 在片段中根本没有调用。.我不知道出了什么问题

活动

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    final DrawerLayout drawer = findViewById(R.id.drawer_layout);
    //Support app bar variations
    mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_fragment1, 
     R.id.nav_fragment2,R.id.nav_fragment3)
            .setDrawerLayout(drawer)
            .build();
    NavigationUI.setupWithNavController(toolbar, navController, mAppBarConfiguration);
    //Add a navigation drawer
    NavigationView navigationView = findViewById(R.id.nav_view);
    NavigationUI.setupWithNavController(navigationView, navController);

// override onSupportNavigateUp() to handle Up navigation:
@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  return NavigationUI.navigateUp(navController, mAppBarConfiguration)
           || super.onSupportNavigateUp();

帧代码

  @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // handle the up button here
        switch (item.getItemId()) {
            case android.R.id.home:
                // handle ⬅️ button here
                showDialog();
                return true;
            default:
//        return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(Fragment.this))
//                || super.onOptionsItemSelected(item);
                return super.onOptionsItemSelected(item);
        }
    }

0 个答案:

没有答案