CAB works fine in an AppCompatActivity.
Can I use it also in an DialogFragment (v4)? If so, what should I use as the context to start the action mode? The next statement does not work:
((AppCompatActivity) MainActivity.myActivity).startSupportActionMode( mcListener);
I know that the code is already in a Dialog.
Starting the action mode on the current activity shows the action bar behind the dialog, so it is inaccessible without dismissing it.
After several trials to avoid this, I've had a look at the source code of AppCompatActivity
, and managed to find a solution (what worked for me):
Created a field,
private AppCompatDelegate delegate;
overrode onStart
like this:
@Override
public void onStart () {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) delegate = AppCompatDelegate.create(dialog, this);
}
and created an action mode starter method
@Nullable
public ActionMode startSupportActionMode (ActionMode.Callback callback) {
if (delegate != null) return delegate.startSupportActionMode(callback);
return null;
}
It can also work overriding onCreateDialog
and create the delegate there, but from the source of DialogFragment
the getDialog
method supposed to return the same (current) dialog instance on onStart
too, so it is ok creating the delegate there.