android - Android Fragment Menus
问题描述
In an app activity, I have four different fragments. I have added menus to each fragment with
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {}
function. But when I swipe to next fragments, and come back to previous, the menu options of the next fragment, get added to the menu in the current visible fragment. Also added the first statement menu.clear()
in the onCreateOptionsMenu()
but no effect. Stuck at this issue.
解决方案
在每个片段OnCreate
和OnCreateView
方法中,确保您setHasOptionMenu(true)
按如下方式调用。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
...
setHasOptionsMenu(true);
return rootView;
}
还要检查你是否在打电话super.onCreateOptionsMenu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.list_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
推荐阅读
- mysql - Laravel 分页不会重新初始化 MySQL 变量
- html - 如何使用Javascript一次只选择两个连续的复选框
- ios - 较小尺寸的uiviewcontroller
- r - 将带引号的字符串放在 rlang 函数中的正确方法是什么?
- unity3d - 在 Photon 中,每个客户端都收到两次引发事件
- reactjs - componentWillUnMount 调度操作上的空数组的酶测试
- php - 后续解密失败,文件大小为 0
- mysql - 使用 MySQL AFTER UPDATE 触发器更新另一个表中的列基于 INT 值更改
- r - 如何将 R 中的列与不同级别的值合并
- bash - 如何在一行bash中通过索引获取特定单词