I have a class extending a DialogFragment and in this I am showing a ListView. I want to have a LongPress option for the items in this list. So I have overridden onCreateContextMenu
and onContextItemSelected
. I have my context menu appearing with the correct options.
The issue I am having is that I can't call getDialog().dismiss()
from within the onContextItemSelected
method.
What is the proper way of closing a class extending DialogFragment, from within the onContextItemSelected
method of the class extending DialogFragment?
/*
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
if(view.getId() == listView.getId())
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
Industry industry = (Industry) listView.getItemAtPosition(info.position);
menu.setHeaderTitle(industry.name);
menu.add(Menu.NONE, USE_INDUSTRY, 0, USE_INDUSTRY_TEXT);
}
}
/*
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onContextItemSelected(android.view.MenuItem)
*/
@Override
public boolean onContextItemSelected(MenuItem item)
{
if(item.getItemId() == USE_INDUSTRY)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
Industry industry = (Industry) listView.getItemAtPosition(info.position);
MyApplication.BUS.post(new IndustryEvent(industry.ID, -2));
getDialog().dismiss();
}
return true;
}
I have found the answer, onContextItemSelected()
was not being called in my Dialog Fragment. Which meant that the getDialog().dismiss()
was not being called.
The only way I can find around this is to set the onMenuItemClickListener
for the MenuItem and register the click there. Below is the final code I am using.
/*
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
if(view.getId() == listView.getId() && isIndustryLevel)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
Industry industry = (Industry) listView.getItemAtPosition(info.position);
menu.setHeaderTitle(industry.name);
menu.add(Menu.NONE, USE_INDUSTRY, 0, USE_INDUSTRY_TEXT);
menu.getItem(0).setOnMenuItemClickListener(new OnUseIndustryButtonClick());
}
}
private class OnUseIndustryButtonClick implements MenuItem.OnMenuItemClickListener
{
/*
* (non-Javadoc)
* @see android.view.MenuItem.OnMenuItemClickListener#onMenuItemClick(android.view.MenuItem)
*/
@Override
public boolean onMenuItemClick(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
Industry industry = app.industries.get(info.position);
MyApplication.BUS.post(new IndustryEvent(industry.ID, -2));
getDialog().dismiss();
return true;
}
}