首页 > 解决方案 > navController.addOnDestinationChangedListener 未提供导航图中使用的活动项

问题描述

我正在尝试在导航菜单中调用注销选项,并且调用工作正常,并且导航到登录屏幕。但是我想清除存储在共享首选项中的登录详细信息,为此,我尝试了`

navController.addOnDestinationChangedListener { controller, destination, arguments ->

            Log.d("TAG", "label: "+destination.label)
            
        }`

但它没有给出注销菜单 id 甚至标签,当我点击注销菜单时它给出了 nav_home 标签/id

这是我的菜单项类

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/dashboard"
            android:title="@string/menu_home" />
    </group>

    <item android:title="@string/recharge">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_mobile_recharge"
                    android:icon="@drawable/ic_mobile"
                    android:title="@string/mobile_Recharge" />
                <item
                    android:id="@+id/nav_dth_recharge"
                    android:icon="@drawable/dth"
                    android:title="@string/dth_recharge" />
                <item
                    android:id="@+id/nav_post_paid_bill_recharge"
                    android:icon="@drawable/postpaid"
                    android:title="@string/post_paid_bill" />
            </group>
        </menu>
    </item>

    <item android:title="@string/my_account">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_change_password"
                    android:icon="@drawable/change_pass"
                    android:title="@string/change_password" />
                <item
                    android:id="@+id/nav_edit_profile"
                    android:icon="@drawable/profile"
                    android:title="@string/edit_profile" />
                <item
                    android:id="@+id/nav_my_commision"
                    android:icon="@drawable/commision"
                    android:title="@string/my_commision" />
            </group>
        </menu>
    </item>

    <item android:title="@string/support">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_share"
                    android:icon="@drawable/customer_service"
                    android:title="@string/support_ticket" />
                <item
                    android:id="@+id/nav_send"
                    android:icon="@drawable/contact_support"
                    android:title="@string/contact_details" />
            </group>
        </menu>
    </item>

    <item android:title="Other">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_logout"
                    android:icon="@drawable/customer_service"
                    android:title="@string/logout" />

            </group>
        </menu>
    </item>

</menu>

这是我的导航图

    <?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.project.wowdth.fragment.HomeFragment"
        android:label="@string/menu_home"
        app:enterAnim="@anim/slide_in"
        app:exitAnim="@anim/slide_out"
        app:popEnterAnim="@anim/nav_default_pop_enter_anim"
        app:popExitAnim="@anim/nav_default_pop_exit_anim"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/navigateToMobileRechargeFragmnt"
            app:destination="@+id/nav_mobile_recharge"
            app:enterAnim="@anim/slide_in"
            app:exitAnim="@anim/slide_out"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim" />

        <action
            android:id="@+id/navigateToDthRechargeFragmnt"
            app:destination="@id/nav_dth_recharge"
            app:enterAnim="@anim/slide_in"
            app:exitAnim="@anim/slide_out"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim" />
        <action
            android:id="@+id/navigate_to_post_paid_recharge"
            app:destination="@id/nav_post_paid_bill_recharge"
            app:enterAnim="@anim/slide_in"
            app:exitAnim="@anim/slide_out"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim" />

    </fragment>

    <fragment
        android:id="@+id/nav_mobile_recharge"
        android:name="com.project.wowdth.fragment.MobileRechargefragment"
        android:label="@string/mobile_Recharge"
        app:enterAnim="@anim/slide_in"

        app:exitAnim="@anim/slide_out"
        app:popEnterAnim="@anim/nav_default_pop_enter_anim"
        app:popExitAnim="@anim/nav_default_pop_exit_anim"
        tools:layout="@layout/fragment_mobile_recharge_layout" />

    <fragment
        android:id="@+id/nav_dth_recharge"
        android:name="com.project.wowdth.fragment.DTHRechargeFragment"
        android:label="@string/dth_recharge"
        tools:layout="@layout/fragment_dth_recharge_layout" />

    <fragment
        android:id="@+id/nav_post_paid_bill_recharge"
        android:name="com.project.wowdth.fragment.PostpaidRechargeFragment"
        android:label="@string/post_paid_bill"
        tools:layout="@layout/fragment_post_paid_recharge_layout" />


    <fragment
        android:id="@+id/nav_edit_profile"
        android:name="com.project.wowdth.fragment.EditProfileFragment"
        android:label="@string/edit_profile"
        tools:layout="@layout/fragment_edit_profile_layout" />

    <fragment
        android:id="@+id/nav_change_password"
        android:name="com.project.wowdth.fragment.ChangePasswordFragment"
        android:label="@string/change_password"
        tools:layout="@layout/fragment_change_password_layout" />

    <fragment
        android:id="@+id/nav_my_commision"
        android:name="com.project.wowdth.fragment.MyCommissionFragment"
        android:label="@string/my_commision"
        tools:layout="@layout/fragment_my_commission_layout" />

    <activity
        android:id="@+id/nav_logout"
        android:name="com.project.wowdth.activity.LoginActivity"
        android:label="@string/logout"
        tools:layout="@layout/activity_login">
        <argument
            android:name="login"
            app:argType="string"
            android:defaultValue="login" />
    </activity>



</navigation>

有人,请帮我整理一下

标签: androidkotlinnavigation-drawer

解决方案


推荐阅读