简体   繁体   English

与多个活动共享导航抽屉

[英]Share Navigation Drawer with multiple Activities

I have a navigation drawer activity. 我有一个导航抽屉活动。 Behind most of the menu items i have various fragment and I only replace the content layout with the fragment like this: 在大多数菜单项的后面,我有各种片段,而我只用这样的片段替换内容布局:

fragmentManager.beginTransaction().replace(R.id.content_menu, new InformationFragment()).commit();

So when I click on a menu item, the navigation drawer slides back to the left, while the content changes to whatever is in the fragment and I can keep using the navigation drawer. 因此,当我单击菜单项时,导航抽屉会向左滑动,而内容会更改为片段中的内容,因此我可以继续使用导航抽屉。

But one of the menu items does require its own Activity, so instead of a transaction to a new fragment I do: 但是菜单项之一确实需要其自己的Activity,因此我不需要处理新片段:

Intent intent  = new Intent(this, NewActivity.class);
startActivity(intent);

In this new activity I can add the same navigation drawer as I use in my first activity and this works, but I cant imagine that this is the best way to handle this situation. 在这个新活动中,我可以添加与我在第一个活动中使用的相同的导航抽屉,并且可以正常工作,但是我无法想象这是处理这种情况的最佳方法。

Is it possible to have the navigation drawer "above" the activities so they can share it? 导航抽屉是否可以在活动上方,以便他们可以共享活动?

I recently implemented this on a project. 我最近在一个项目上实现了这一点。 My main inspiration for the implementation came from the Google I/O official app . 实现的主要灵感来自Google I / O官方应用程序

To summarize the main points: 总结要点:

  • Create an AppNavigationView that holds all the logic for displaying navigation items, closing or opening the drawer and handling click events 创建一个AppNavigationView ,其中包含用于显示导航项,关闭或打开抽屉以及处理单击事件的所有逻辑
  • Create a BaseActivity that has a an AppNavigationView member 创建BaseActivity具有的AppNavigationView成员
  • Make sure that each activity that requires a navigation drawer extends the BaseActivity you just created 确保每个需要导航抽屉的活动都扩展了您刚创建的BaseActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM