Search code examples
androidandroid-dialogandroid-dialogfragmentcontextual-action-barandroid-actionmode

How to close the text selection contextual action bar programmatically?


EDIT:

Initial problem:

I have a fragment that extends a DialogFragment. In this fragment I create an AlertDialog and set an adapter. The adapter uses a custom layout to make the TextViews selectable.

Now it's all fine and dandy but after selecting some text and dismissing the dialog, later the text is selected but the action bar is not shown anymore. It can be fixed only by re-creating the activity which starts the fragment.

tl;dr: Using a static dialog. After text selection and dismiss(), the text can be selected but the CAB is nowhere to be found. Perhaps closing the CAB programatically can solve this. How would I do that?

General question:

I would like to find out how I can manually close the default android text selection CAB (or get a reference to it).


Solution

  • I solved my initial problem by creating a new dialog for each fragment. Using a static dialog somehow messed up my text selection. It's too bad though as I now have to do a bunch of adjustments everytime I start the DialogFragment.

    Now for my general question I solved that by setting ActionMode callbacks to each convertView of my adapter. That way I was able to close the ActionMode anytime between getting the reference to the mode (onCreateActionMode) and clearing the reference (onDestroyActionMode). Here's the code:

    public ActionMode mActionMode;
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // (Re)Use the convertView
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.popup_list_item, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.popupItem);
            holder.textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mActionMode = mode;
                    // Can now use the mode whenever (if it's not null)
                    // e.g. call mActionMode.finish()
                    return true; // true = create the ActionMode
                }
    
                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }
    
                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }
    
                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    mActionMode = null;
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    
        // Set text
        if (mQuery != null)
            holder.textView.setText(Html.fromHtml(getItem(position)));
        else
            holder.textView.setText(getItem(position));
    
        return convertView;
    }
    

    However I failed to solve my initial problem (even after being able to close the ActionMode manually), so I was forced to drop the usage of a static Dialog.

    I am open to suggestions, on how to solve my initial problem, if anyone has any.