什么是处理主/详细流程菜单项的正确方法?

问题描述:

我建的基础上产生的Andr​​oid工作室例如一个主/详细流程。我使用 setHasOptionsMenu(真)让我的列表和详细的碎片有助于操作栏菜单。问题是,如果用户切换回从横向纵向那么细节片段仍然存在,仍然有助于操作栏菜单的时候真的只有列表中显示。什么是执行菜单项,这样当片段没有明显present他们不会流连的正确方法?

I built a master/detail flow based on the generated Android Studio example. I am using setHasOptionsMenu(true) to allow my list and detail fragments to contribute to the action bar menu. The problem is that if the user switches back to portrait from landscape then the detail fragment is still around and still contributing to the action bar menu when really only the list is showing. What is the proper way to implement menu items such that they won't linger when the fragment is not visibly present?

一个解决方案是使用的可见性()检查细节片段的 onCreateOptionsMenu 来有条件地膨胀菜单。

One solution is to use an isVisible() check in the detail fragment's onCreateOptionsMenu to conditionally inflate the menu.