How to Handle Oncontextitemselected in a Multi Fragment Activity

How to handle onContextItemSelected in a multi fragment activity?

I found an alternative. It does not change anything on my problem above, but it makes it pointless.

I have remove the context menu completely from my application. Instead I capture the longclick on a list item and change the visible buttons of the action bar in this moment.
From the user point of view this is much more tablet like as a context menu.

In backward compatible applications the actionbar does not exist. So I've decided to build my own (kind of toolbar on top) for the devices pre Honeycomb.

If you would like to stay with the context menu, I did not find a better solution as the workaround I've mentioned above.

Wrong fragment in ViewPager receives onContextItemSelected call

So this is some sort of idiotic design decision by Google or something that has just gone totally unconsidered. The simplest way to work around this is to wrap the onContextItemSelected call with an if statement like this:

if (getUserVisibleHint()) {
// Handle menu events and return true
} else
return false; // Pass the event to the next fragment

The compatibility library in ActionBarSherlock 3.5 had a hack like this.

onContextItemSelected called twice for fragment

This solution on this question fixed my issue:

How to handle onContextItemSelected in a multi fragment activity?

using getUserVisibleHint() in onContextItemSelected.

Inappropriate Context Menu within a Fragment

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.

Using Options Menu in a multi-Fragment Activity

My solution was to use the setOnPageChangeListener method of the ViewPager in order to keep track of which Fragment index was currently visible with the onPageSelected callback. You still have to get the initial index yourself, but in my case this was trivial

Multiple context menus in one activity based on selected list view item

first way:The ContextMenu.ContextMenuInfo there is extra information about the item for which the context menu should be shown. This information will vary depending on the class of v. so you can do this , copy and paste this

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo info) {
super.onCreateContextMenu(menu, v, info);
int index = info.position; //The position in the adapter for which the context menu is being displayed.
View child = info.targetView;//The child view for which the context menu is being displayed.
// so for your case you have to use the child,because of your way about it
if(child.getId() == R.id.first_button)
inflate one menu
else if(child.getId() == R.id.second_button)
inflate another menu
}

let me know if its useful

Context Menu in Fragment uses ListView from a different Fragment: registerForContextMenu(getListView())

I managed to figure this out finally!

It turns out it wasn't just calling the context menu for the wrong ListView, the context menu was being called for ALL fragments.

This was solved by using getUserVisibleHint() in an if statement within the onContextItemSelected method, so that if the Fragment the context menu was called for was not visible, it would return false, but if it was the currently visible Fragment, it would execute the proper code, meaning is skips over Fragments that are not the intended Fragment. I had tried getUserVisibleHint() already in a different way but I wasn't thinking about the problem from the right angle then.

Here's an example of the solution.

@Override
public boolean onContextItemSelected(MenuItem item) {
if (getUserVisibleHint()) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int dataID = data.get(info.position).getDataID();
String dataName = data.get(info.position).getDataName();

Activity activity = getActivity();
if(activity instanceof MainActivity) {
switch (item.getItemId()){
case R.id.context_ringtone:
((MainActivity) activity).setRingtone(dataID, dataName);
return true;
case R.id.context_notification:
((MainActivity) activity).setNotification(dataID, dataName);
return true;
case R.id.context_alarm:
((MainActivity) activity).setAlarm(dataID, dataName);
return true;
case R.id.context_sd_card:
((MainActivity) activity).saveFile(dataID, dataName);
return true;
default:
return super.onContextItemSelected(item);
}
}
}
return false;
}


Related Topics



Leave a reply



Submit