My MainActivity использует onNavigationItemSelected(MenuItem item)
, включающее действие. Но так или иначе это не откроет другие фрагменты, которые у меня есть на ящике. Я могу открыть фрагменты, если я удаляю navigationView.setNavigationItemSelectedListener(this)
. Но тогда я не могу открыть BookingActivity.
Моя основная активность:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//region Properties
private AppBarConfiguration mAppBarConfiguration;
ImageButton settings_btn;
//endregion
@Override
protected void onStart() {
super.onStart();
ProductDatabase productDb = new ProductDatabase();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
settings_btn = findViewById(R.id.btn_settings);
settings_btn.setOnClickListener(v ->
startActivity(new Intent(MainActivity.this, SettingsActivity.class)));
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_login, R.id.nav_treatments, R.id.nav_product, R.id.nav_about, R.id.nav_contactUs)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(this); // Fragments work if I remove this line, but not BookingActivity
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_booking) {
startActivity(new Intent(this, BookingActivity.class));
} else if (id == R.id.nav_home) {
} else if (id == R.id.nav_product) {
// What to write here to open ProductFragment?
} else if (id == R.id.nav_treatments) {
} else if (id == R.id.nav_about) {
} else if (id == R.id.nav_contactUs) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}