Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inappropriate Context Menu within a Fragment

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.

like image 991
Dennis Avatar asked Dec 29 '13 12:12

Dennis


People also ask

Why are Fragment menu items ordered before Activity Menu items?

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?

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

How to hide the Fragment menu items in Android?

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.

How to inflate a Fragment menu layout XML?

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.


1 Answers

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.

like image 191
Dennis Avatar answered Sep 23 '22 19:09

Dennis