I am creating a application which contains Navigation Drawer Activity. I am loading different Fragments in my main Screen. Now when I have to call Fragment at that time it w
When you call NavigationUI.setupWithNavController(navigationView, navController)
, you're saying that you want NavController
to handle click events from your NavigationView, navigating to the related screen as per the NavigationUI documentation. This, by necessity, calls setNavigationItemSelectedListener()
internally.
By calling setNavigationItemSelectedListener
afterwards, you remove the original listener, which is why your other items don't do anything anymore. You can trigger the default behavior by calling NavigationUI.onNavDestinationSelected()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.callUs) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "XXXxxxXXX"));
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
}
else
{
// Make your navController object final above
// or call Navigation.findNavController() again here
NavigationUI.onNavDestinationSelected(menuItem, navController);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}