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 9322013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:59:02+00:00 2026-06-19T03:59:02+00:00

My app has a ListView and I’m using a Contextual Action Bar for devices

  • 0

My app has a ListView and I’m using a Contextual Action Bar for devices above SDK 11 and the old popup contextual actions for older devices. I know there’s a way to use a CAB with older devices but I tried to implement it and found it wasn’t worth the effort for devices that will eventually be obsolete. I know it’s some code duplication, but, in theory, I will be getting rid of the old popup actions (emphasis on “in theory”).

Anyhow, when I use the emulator, the CAB works fine, but the old popup actions for older devices seems to hit onContextItemSelected twice when I put a breakpoint in that event. I’ve just start implement a ViewPager for my app and this wasn’t happening before the ViewPager so not sure if that is causing the issue.

This is the code I’m using:

public class MyFragment extends SherlockListFragment
{
    private ListView mListView;
    private android.view.ActionMode mActionMode;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) 
    {
          super.onCreateContextMenu(menu, v, menuInfo);

         if (this.mActionMode != null) return;

          menu.add(1, 0, 0, "Delete");
          menu.add(1, 1, 0, "Save");
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        mListView = getListView();

        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
        {   
            mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

            mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

                @Override
                public boolean onCreateActionMode(android.view.ActionMode mode, android.view.Menu menu) {
                    // Inflate the menu for the CAB
                    menu.clear();
                    menu.add(1, 1, 2, "Delete").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                    menu.add(1, 3, 1, "Save").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

                    return true;
                }

                @Override
                public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {

                    mActionMode = mode;

                    if (item.getGroupId() == 1) 
                    {
                        switch(itemId)
                        {
                            case 0:
                                DeleteItem();
                                break;
                            case 1:
                                SaveItem();
                                break;
                        }
                    }
                }
            }
        }
    }

    @Override
    public boolean onContextItemSelected(final android.view.MenuItem item) {

        if (item.getGroupId() == 1) {

            final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
            final Integer position = info.position;
            final int itemId = item.getItemId();

            switch(itemId)
            {
                case 0:
                    DeleteItem();
                    break;
                case 1:
                    SaveItem();
                    break;
            }
        }

        return super.onContextItemSelected(item);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {

        super.onPrepareOptionsMenu (menu);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.my_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }
}

ViewPager code

public class Main extends SherlockFragmentActivity
{
    private static List<Integer> mIds;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(null);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout

        if (mViewPager != null)
        {
            mIds = new ArrayList<Integer>();

            mIds.add(0);
            mIds.add(1);
            mIds.add(2);
        }
        else //in landscape
        {           
            ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);

            if (lf == null)
                lf = new ListFragment();

            DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);

            if (df == null)
            {
                df = new DetailFragment();
                df.setArguments(getIntent().getExtras());   
            }

            getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();
        }
    }       

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {        
            //can't use getSupportFragmentManager().findFragmentById() here because I get a "Cannot make a static reference to the non-static method" error
            if (index == 0)
                return ListFragment.newInstance();
            else            
                return DetailFragment.newInstance(mIds.get(index-1));
        }  

        @Override
        public int getCount() {  
             return 4;
        }
   }  
}
  • 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-19T03:59:03+00:00Added an answer on June 19, 2026 at 3:59 am

    This solution on this question fixed my issue:

    How to handle onContextItemSelected in a multi fragment activity?

    using getUserVisibleHint() in onContextItemSelected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app has a main menu, and I am using a ListView to represent
I am new to using ViewPagers and Fragments together. My app has a ListView
I am writing an app that has a ListView of text. In each row
I'm making an app that has a listview with a child listview for each
I'm working on an app that has a listview, and I need a way
I'm working with a winForms app that has a listView. That listView has multi-select
I have an android-app with a listview in an activity. The listview has, if
My app has several RelativeLayout s with a TextView and a ListView in each.
My app uses HoneyComb fragments where my left side panel has listview and right
There is a ListView in my App and the ListView has a selector. I

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.