所以我使用 Navgraph 来设置我的抽屉导航目的地,我想知道如何在使用 NavGraph 而不是执行 NavigationView.setOnNavigationItemSelectedListener 然后执行 if/else 语句或 switch 语句来单击菜单 ID 时执行方法。具体来说,我想在单击注销按钮时注销用户(方法),然后使用导航图转到登录页面。用更简单的术语来说,即使在我使用导航图和导航控制器设置抽屉导航之后,我如何才能对菜单项执行操作。
这是与我的活动中的导航有关的代码:
//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);
这是我要使用的方法:
public void signOutFromBase(){
signOutAuth.signOut();
}
这是我的导航图 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/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>
这是我的抽屉导航 XML 文件:
<?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>
您的帮助将不胜感激
更新
我决定将注销按钮作为一个操作栏项目,它将为我执行注销。但是,如果有人仍然可以回答我的问题,那将是惊人的,因为现在我想不出任何方法来使用导航图来执行除了传递数据之外的操作。