7

What I have done:

I have created Navigation Drawer Activity, As updated new format of Navigation Drawer Activity, As per new Android architecture I got it with Navigation Component structure.

The NavigationView code with NavController and NavigationUI as below which is opening fragment when I click on any navigation item.

    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_profile, R.id.nav_privacy_policy,
            R.id.nav_terms, R.id.nav_contact_us, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

This is for nav_host_fragment:

<fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

The navigation is happening using this navigation/mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.sohamerp.marsremedies.fragment.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_profile"
        android:name="com.sohamerp.marsremedies.fragment.ProfileFragment"
        android:label="@string/menu_my_profile"
        tools:layout="@layout/fragment_profile" />

    <fragment
        android:id="@+id/nav_privacy_policy"
        android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
        android:label="@string/menu_privacy_policy"
        tools:layout="@layout/fragment_privacy_policy" />

    <fragment
        android:id="@+id/nav_terms"
        android:name="com.sohamerp.marsremedies.fragment.TermsConditionFragment"
        android:label="@string/menu_terms"
        tools:layout="@layout/fragment_terms_condition" />

    <fragment
        android:id="@+id/nav_contact_us"
        android:name="com.sohamerp.marsremedies.fragment.ContactUsFragment"
        android:label="@string/menu_contact_us"
        tools:layout="@layout/fragment_terms_condition" />

</navigation>

Where I am getting problem:

The Android Studio has create fragments for all the 6 menus but I dont want to open fragment on click of last two items. So for that I removed last two <fragment> tags from mobile_navigation.xml

enter image description here

I have tried to add setNavigationItemSelectedListener of below.

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.nav_share)
                Toast.makeText(NavigationMenuActivity.this, "Sharing...", Toast.LENGTH_SHORT).show();
            else if (menuItem.getItemId() == R.id.nav_send)
                Toast.makeText(NavigationMenuActivity.this, "Rating...", Toast.LENGTH_SHORT).show();
            return false;
        }
    });

The Toast is displaying when I click on last two menu but the first 4 menu are not working.

What can I do to make it working?

build.gradle dependency:

implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

3 Answers3

5

You can make a switch statement and for 4 other fragments you should return true.

    switch(menuItem.getItemId) {
      case R.id.import:
          navController.navigate(R.id.nav_home)
          return true
      case R.id.nav_gallery:
          navController.navigate(R.id.nav_gallery)
          return true
      case R.id.nav_slideshow:
          navController.navigate(R.id.nav_slideshow)
          return true
      case R.id.nav_tools:
          navController.navigate(R.id.nav_tools)
          return true
      case R.id.nav_share:
                Toast.makeText(NavigationMenuActivity.this, "Sharing...", Toast.LENGTH_SHORT).show();
          return false;
  case R.id.nav_send: 
                Toast.makeText(NavigationMenuActivity.this, "Rating...",Toast.LENGTH_SHORT).show();
          return false;
      default:
          return false;
}
}

And with overriding onBackPressed you can achieve default behavior. (Note: The default behavior is just back once to first fragment then after its closing activity)

public void onBackPressed() {

 if (navigationDrawer.isDrawerOpen(GravityCompat.START)) {
            navigationDrawer.closeDrawer(GravityCompat.START)
    } else {
          if (navController.currentDestination?.id == R.id.nav_home){
              finish()
          } else {
           navController.navigate(R.id.action_global_nav_home)
           drawerView.menu.findItem(R.id.menu_item_nav_home).isChecked = true
          }

And in navigation/mobile_navigation.xml add a global action:

 <action
        android:id="@+id/action_global_nav_home"
        app:destination="@id/nav_home" />
bromden
  • 284
  • 3
  • 12
  • Not working. Its clickable but not changing fragment. – Pratik Butani Dec 23 '19 at 13:40
  • I have edited answer, just check the ids, and maybe it can be written in a more concise way, but this is the idea. – bromden Dec 23 '19 at 15:15
  • Yes, Its worked but its appending stack of fragments so whenever I press back button, its opening all the fragments which is opened previously. `(Note: The default behavior is just back once to first fragment then after its closing activity. I want this behavior.)` – Pratik Butani Dec 24 '19 at 05:41
  • Overriding onBackPressed you can achieve that behavior. – bromden Dec 24 '19 at 08:52
  • `super.onBackPressed();` doing same, Its removing one by one fragment from stack and at the last finishing when `home` fragment is appeared – Pratik Butani Dec 24 '19 at 09:01
1

I got the solution as below:

    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top-level destinations.
    mAppBarConfigurationLeft = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_outstanding, R.id.nav_profile, R.id.nav_privacy_policy,
            R.id.nav_terms, R.id.nav_contact_us, R.id.nav_share, R.id.nav_send)
            .setOpenableLayout(binding.drawerLayout)
            .build();
    navControllerLeft = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navControllerLeft, mAppBarConfigurationLeft);
    NavigationUI.setupWithNavController(binding.navView, navControllerLeft);

    setUpHeaderView();

    /**
     * Clear Mapping code if Salesman or Admin has logged in
     */
    binding.navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.nav_home:
                    navControllerLeft.navigate(R.id.nav_home);
                    break;
                case R.id.nav_outstanding:
                    navControllerLeft.navigate(R.id.nav_outstanding);
                    break;
                case R.id.nav_profile:
                    navControllerLeft.navigate(R.id.nav_profile);
                    break;
                case R.id.nav_privacy_policy:
                    navControllerLeft.navigate(R.id.nav_privacy_policy);
                    break;
                case R.id.nav_terms:
                    navControllerLeft.navigate(R.id.nav_terms);
                    break;
                case R.id.nav_contact_us:
                    navControllerLeft.navigate(R.id.nav_contact_us);
                    break;
                case R.id.nav_share:
                    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
                    sharingIntent.putExtra(Intent.EXTRA_TEXT, "Here I am sharing Skites App. Download here: https://play.google.com/store/apps/details?id=" + getPackageName());
                    sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(Intent.createChooser(sharingIntent, getString(R.string.app_name)));
                    break;
                case R.id.nav_send:
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(NavigationMenuActivity.this, "No Application Found to Rate", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
            binding.drawerLayout.closeDrawer(GravityCompat.START, true);
            return false;
        }
    });

Hope it will helps, Thank you.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

I found a pretty hacky solution that worked relatively painlessly. I overrided the NavigationView with it's setNavigationItemSelectedListener Here is a Ktlin example:

import android.content.Context
import android.util.AttributeSet
import android.view.MenuItem
import com.google.android.material.navigation.NavigationView

/**
 * Created by EdgarK on 01/08/2020.
 */
class SKNavigationView@JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : com.google.android.material.navigation.NavigationView(context, attrs, defStyleAttr),
    NavigationView.OnNavigationItemSelectedListener {
    private var originalListener : OnNavigationItemSelectedListener? = null
    var menuItemNotHandledListener : OnNavigationItemSelectedListener? = null;

    override fun setNavigationItemSelectedListener(listener: OnNavigationItemSelectedListener?) {
        originalListener = listener
        super.setNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val handled = originalListener?.onNavigationItemSelected(item) ?: false
        return handled || menuItemNotHandledListener?.onNavigationItemSelected(item) ?: false
    }
}

Then you can set menuItemNotHandledListener that will only be called if the original one didn't have a fragment.

EdgarK
  • 870
  • 7
  • 10