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.