I have an activity with a Navigation Drawer and a fragment. Each of them has his own ListView with a Contextual Menu.
The Context Menu inside the navigation drawer works fine.
The problem is with the menu inside the fragment. When I enter the fragment's context menu, BOTH navigation drawer's menu AND fragment's menu are displayed.
Activity's methods
@Override
protected void onCreate(Bundle savedInstanceState) {
registerForContextMenu(mDrawerList); // mDrawrList is the navigation drawer's ListView
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.action_edit:
// Do some stuff
return true;
case R.id.action_delete:
// Do some stuff
return true;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.drawer, menu);
}
Fragment's methods
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
mListView = (ListView) view.findViewById(R.id.list);
registerForContextMenu(mListView);
// ...
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.action_edit:
// Do some stuff
return true;
case R.id.action_delete:
// Do some stuff
return true;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
}
Thank you in advance.
When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item’s android:orderInCategory attribute value.
How to use context in a fragment? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken two fragments. Step 3 − Add the following code to src/MainActivity.java
This is because of the menu item’s android:orderInCategory attribute value. When you click the hide button to hide the fragment. The fragment menu items disappear from the action bar also. You can also click back menu to exit the fragment and the activity.
Override Fragment class onCreateOptionsMenu (Menu menu, MenuInflater inflater) method, inflate the fragment menu layout xml file in this method as below. 2. How To Handle Fragment Menu Item Click Event.
Solved.
The problem was that the activity's onCreateContextMenu
was called.
I just removed super.onCreateContextMenu(menu, v, menuInfo);
from the fragment's onCreateContextMenu
method.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
}
Hope it'll help someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With