Android Navigation Drawer click Event Issue

前端 未结 2 2080
死守一世寂寞
死守一世寂寞 2021-01-15 23:40

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

2条回答
  •  伪装坚强ぢ
    2021-01-16 00:16

    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;
    

    }

提交回复
热议问题