Daksh
Daksh

Reputation: 1177

Android ActionMode replace Action Items dynamically

I have implemented an ActionMode via ActionBarSherlock (thanks Jake Wharton for ABS!).

Anyways, i am calling the mActionMode.invalidate(); each time i click on a ListView item, which also increments/decrements a checkedCounter.

Then inside the public boolean onPrepareActionMode(ActionMode mode, android.view.Menu menu) , I have to set the ActionMode contents according to the value of the checkedCounter.

Code for the onPrepareActionMode is below :

    public boolean onPrepareActionMode(ActionMode mode,
                android.view.Menu menu) {
            Log.d("ASDASD", "INSIDE ONPREPARE!");
            if (mActionMode == null) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.contextualmultiple, menu);
                return false;
            }
            if (checkedCount == 0) {
                mActionMode.finish();
            } else if (checkedCount == 1 && mActionMode != null) {
                mActionMode.setTitle(checkedCount + " Message Selected");
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.contextualmultiple, menu);
                return true;
            } else if (checkedCount > 1) {
                mActionMode.setTitle(checkedCount + " Messages Selected");
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.contextual, menu);
                return true;
            }
            return true;
        }

NOW, the problem is that, when the checkCount is 1, it inflates the menu with say, 3 items in it. But, when the counter increments to 2, it inflates the other menu with 1 item, BUT adds on to the previously inflated menu, without nullifying the contents first. I.E.; now the ActionMode has 2 of previous + 1 of current in the Contextual ActionBar!

How can i change my code to remove the previously existing menu items from the ActionMode when the mActionMode.invalidate() is called?

P.S. : the onCreateActionMode() function is empty, as the onPrepareActionMode() does everything, including when the mActionMode is null and has a menu to be inflated for the first time.

Upvotes: 3

Views: 4913

Answers (1)

Daksh
Daksh

Reputation: 1177

haha! i found a ridiculously simple solution to the ridiculous problem!

for anyone else that MIGHT encounter this problem, just add a menu.clear(); statement before all inflate() statements, and that's it!

Upvotes: 8

Related Questions