Android InvalidateOptions功能表未触发OnPrepareOptions功能表()

Android InvalidateOptions功能表未触发OnPrepareOptions功能表(),android,kotlin,menu,menuitem,Android,Kotlin,Menu,Menuitem,在主活动中,我只是尝试在运行时(minSdkVersion 23,targetSdkVersion 29)更改“基本菜单”(抽屉布局、NavigationView、menu.xml、活动中的setNavigationItemSelectedListener等)项的标题 基本上,共享首选项中有一个变量引用“已登录用户”,我希望将该字符串设置为项目的标题,或者在用户“断开连接”时恢复默认项目标题(存储在string.xml中) 正如其他Q/A中所建议的,例如,我尝试调用invalidateOptio

在主活动中,我只是尝试在运行时(minSdkVersion 23,targetSdkVersion 29)更改“基本菜单”(抽屉布局、NavigationView、menu.xml、活动中的setNavigationItemSelectedListener等)项的标题

基本上,共享首选项中有一个变量引用“已登录用户”,我希望将该字符串设置为项目的标题,或者在用户“断开连接”时恢复默认项目标题(存储在string.xml中)

正如其他Q/A中所建议的,例如,我尝试调用invalidateOptions菜单() 它应该触发onPrepareOptions菜单(),我已经用我的“逻辑”覆盖了它

主活动的导航视图

<androidx.drawerlayout.widget.DrawerLayout.....>
........
<com.google.android.material.navigation.NavigationView
    android:id="@+id/main_drawer_navigationView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@drawable/background_gradient_orange"
    android:fitsSystemWindows="false"
    app:itemTextColor="@android:color/white"
    app:itemIconTint="@android:color/white"
    app:menu="@menu/drawer_menu"
    app:headerLayout="@layout/nav_header_main"/>
</androidx.drawerlayout.widget.DrawerLayout>
问题似乎在于onPrepareOptions菜单()从未被激发,即使在创建活动或调用InvalidateOptions菜单()时也是如此

我也尝试过:override-fun-oncreateoptions-menu(menu:menu?):Boolean{return-true},但什么也没发生。我能让它工作的唯一方法就是用“手动”完成它


但是我不太喜欢这个解决方案

A
NavigationView
,它的菜单与选项菜单完全无关。它们是完全不同的东西。也就是说,你想做的只是不起作用。您的“手动”解决方案正是如何在运行时修改
导航视图的菜单。该死,明白了。老实说,我现在觉得自己很笨。非常感谢。
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
    sharedPreferences.getString(CURRENT_EXT_USER,null)?.let {
        menu?.findItem(R.id.menu_item_change_user)?.title = it
    }?: kotlin.run {
        menu?.findItem(R.id.menu_item_change_user)?.title = getString(R.string.item_change_user)
    }
    return super.onPrepareOptionsMenu(menu)
}
main_drawer_navigationView.menu.findItem( R.id.menu_item_change_user).title = "user"