Navigation Components: Switch Menu of Navigation Drawer (for logged in users)

跟風遠走 提交于 2020-02-06 08:16:51

问题


I am using the new Navigation Components library. I have a Navigation Drawer in place that works perfectly fine.

If my user logs in I want to change the menu of the Navigation Drawer. Specifically, I want to change the "Login" item to "Logout" from within a fragment.

All the solutions I found online didn't use Navigation Components. So therefore, they recommended something like this:

// Get the navigationView and swap the menu
NavigationView navigationView = view.findViewById(R.id.nav_view);
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer_logged_in);

However, this doesn't seem to work in my case (I cannot get the navigationView from within a fragment). How does one achieve this with Navigation Components?


回答1:


You can access Activity from Fragment through interface:

  1. Create and interface say LoginSuccessListener
  2. Implements this in your Activity
  3. Inside fragment receive this listener inside onAttach
  4. Use it as you needed to update Navigation Menu


来源:https://stackoverflow.com/questions/58473262/navigation-components-switch-menu-of-navigation-drawer-for-logged-in-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!