So i am using a Navgraph to set my drawer navigation destinations and I want to know how I can execute a method when a menu id is clicked by using a NavGraph instead of doing NavigationView.setOnNavigationItemSelectedListener and then if/else statements or switch statements. Specifically, I want to sign out the user (THE METHOD) when the logout button is clicked and use the navigation graph to then go to the Login Page. In even simpler terms, how can I perform an action on a menuitem even after I have set the Drawer Navigation with a Nav Graph and Nav Controller.
Here is the code that pertains to navigation in my activity:
//set up naviagtion drawer
DrawerLayout drawer = findViewById(R.id.base_drawer_layout);
NavigationView navigationView = findViewById(R.id.base_navigation_drawer);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.drawernav_home, R.id.drawernav_person)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
Here is the method that I want to use:
public void signOutFromBase(){
signOutAuth.signOut();
}
Here is my NavGraph XML file:
<?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/nav_graph"
app:startDestination="@id/drawernav_person">
<fragment
android:id="@+id/drawernav_home"
android:name="com.example.chatter.BasePackage.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/drawernav_person"
android:name="com.example.chatter.BasePackage.UserProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_user_profile" />
<fragment
android:id="@+id/drawernav_base_notifications"
android:name="com.example.chatter.BasePackage.InboxFragment"
android:label="Inbox"
tools:layout="@layout/fragment_inbox" />
<activity
android:id="@+id/infoHolderActivity"
android:name="com.example.chatter.InfoPackage.InfoHolderActivity"
android:label="activity_info_holder"
tools:layout="@layout/activity_info_holder" />
<activity
android:id="@+id/drawernav_base_logout"
android:name="com.example.chatter.LoginPackage.WelcomeHolderActivity"
android:label="Welcome"
tools:layout="@layout/activity_welcome_holder" />
</navigation>
Here is my Drawer Navigation XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Destinations"
>
<menu>
<item
android:id="@+id/drawernav_person"
android:title="Profile"
android:icon="@drawable/person_icon"
/>
<item
android:id="@+id/drawernav_home"
android:title="Home"
android:icon="@drawable/home_icon"
/>
<item
android:id="@+id/drawernav_base_listeners"
android:title="Listeners"
android:icon="@drawable/ear_icon_blue_48"
/>
<item
android:id="@+id/drawernav_base_groups"
android:title="Groups (Coming Soon)"
android:icon="@drawable/people_blue_icon"
/>
<item
android:id="@+id/drawernav_base_notifications"
android:title="Notifications"
android:icon="@drawable/bell_blue_icon"
/>
</menu>
</item>
<item
android:title="Info"
>
<menu>
<item
android:id="@+id/drawernav_base_terms"
android:title="Terms and Conditions"
android:icon="@drawable/terms_blue_icon"
/>
<item
android:id="@+id/drawernav_base_privacypolicy"
android:title="Privacy Policy"
android:icon="@drawable/privacy_blue_icon"
/>
<item
android:id="@+id/drawernav_base_about"
android:title="About"
android:icon="@drawable/about_blue_icon"
/>
</menu>
</item>
<item
android:title="Actions"
>
<menu>
<item
android:id="@+id/drawernav_rate"
android:title="Rate"
android:icon="@drawable/star_blue_icon"
/>
<!-- below is the menu item i have that corresponds to taking you to the logout fragment in my nav graph -->
<item
android:id="@+id/drawernav_base_logout"
android:title="Sign Out"
android:icon="@drawable/exit_app_blue_icon"
/>
</menu>
</item>
</menu>
Your help would be appreciated
UPDATE
I have decided to just put the logout button as an Actionbar item that will perform the logout for me. However, if anyone can still answer my question that would be amazing as right now I can not think of any way to use a nav graph to do actions other than passing data.