我在ViewPager中有两个片段。每个片段都有自己的选项菜单。当我运行应用程序时,首先显示片段一。但是片段中没有显示选项菜单。当我切换到片段时,将显示两个选项菜单。请帮助我。
这是我的片段之一
public class FragmentUz extends Fragment implements MyAdapter.OnItemClickListener{
public FragmentUz() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_uz, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = view.findViewById(R.id.toolbar_one);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_uz, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
findFromDb(s);
return true;
}
@Override
public boolean onQueryTextChange(String s) {
findFromDb(s);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
}