[英]drawer menu opening activities and fragments
Right now some of my menu items open only fragments, how do I get them to open activities as well.现在我的一些菜单项只打开片段,我如何让它们也打开活动。 At the moment I'm using the NavController from Navigation Graph to open the fragments目前我正在使用 Navigation Graph 中的 NavController 打开片段
MainActivity.java MainActivity.java
NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menuLeave:
Toast.makeText(MainActivity.this, "adeus", Toast.LENGTH_SHORT).show();
break;
case R.id.menuAccount:
getSupportFragmentManager().beginTransaction()
.replace(R.id.navHostFragment, new ContaFragment())
.commit();
break;
case R.id.menuPrincipal:
getSupportFragmentManager().beginTransaction()
.replace(R.id.navHostFragment, new PrincipalFragment())
.commit();
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
```
1) You should read this answer and follow those steps https://stackoverflow.com/a/67389269/12660050 1)您应该阅读此答案并按照这些步骤https://stackoverflow.com/a/67389269/12660050
2) Set intent to any particular menu item. 2)将意图设置为任何特定的菜单项。
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
//put your activity intent here
Intent intent = new Intent(MainActivity.this,DestinationActivityName.class);
startActivity(intent);
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.