when my dialog fragment is hide, dispatchKeyEvent worked fine
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    Toast.makeText(FragmentPlayer.this, "test: called", Toast.LENGTH_SHORT).show();
    return super.dispatchKeyEvent(event);
}
but when my dialog fragment is show, dispatchKeyEvent not called
MyDialogFragment mFragment = new MyDialogFragment();
mFragment.show(getSupportFragmentManager(), "MyDialog");
why?
No need to change your DialogFragment code to Dialog, you can do something like this (In case still needed). Using OnKeyListener will solve your problem.
public class BaseDialogFragment extends AppCompatDialogFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            /* Your logic, you get the KeyEvent*/
            return false;
        }
    });
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With