Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:22:14+00:00 2026-06-15T10:22:14+00:00

I have a class extending a DialogFragment and in this I am showing a

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:22:15+00:00Added an answer on June 15, 2026 at 10:22 am

    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;
        }       
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have class animal and classes cat and dog extending it. I want
I have created a custom dialog box extending the DialogFragment class. As stated in
I have a class extending JPanel that I want to embed into a JFrame
I have class SuperYo extending class Persona, like this: public class SuperYo extends Persona
Say, I have a class Foo, extending class Bar. And I want to slightly
I'm using 4list views with class extending Activity. I have to perform different list
I have an abstract class AbstractEvent and some real classes extending it. I want
I have a class extending android.os.Handler . An instance of this handler is passed
I have a custom class extending NSObject . I am maintaining NSMutableArray of this
I have an intermediary class extending System.Web.UI.Page for all of my pages that require

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.