Android 单击抽屉导航项时尝试使用NavGraph执行方法
因此,我使用Navgraph设置抽屉导航目的地,我想知道如何在单击菜单id时使用Navgraph执行方法,而不是使用NavigationView.setOnNavigationItemSelectedListener,然后使用if/else语句或switch语句。具体来说,我想在单击注销按钮时注销用户(方法),然后使用导航图转到登录页面。更简单地说,即使我已经用导航图和导航控制器设置了抽屉导航,如何在菜单项上执行操作 以下是与“我的活动”中的导航相关的代码:Android 单击抽屉导航项时尝试使用NavGraph执行方法,android,navigation-drawer,android-navigation-graph,Android,Navigation Drawer,Android Navigation Graph,因此,我使用Navgraph设置抽屉导航目的地,我想知道如何在单击菜单id时使用Navgraph执行方法,而不是使用NavigationView.setOnNavigationItemSelectedListener,然后使用if/else语句或switch语句。具体来说,我想在单击注销按钮时注销用户(方法),然后使用导航图转到登录页面。更简单地说,即使我已经用导航图和导航控制器设置了抽屉导航,如何在菜单项上执行操作 以下是与“我的活动”中的导航相关的代码: //set up naviagtio
//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();
}
以下是我的NavGraph 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 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>
这是我的抽屉导航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 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>
谢谢你的帮助
更新
我已经决定把注销按钮作为一个操作栏项目,为我执行注销。然而,如果有人仍然能回答我的问题,那将是令人惊讶的,因为现在我想不出任何方法来使用导航图进行除传递数据以外的操作。使用安全参数是答案吗?