I was previously extending the Dialog
class for my dialogs and I had to use the following code segment in my dialogs so that they would not be dismissed when the phone's search key was pressed:
setOnKeyListener(new OnKeyListener()
{
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_SEARCH)
return true; // pretend we've processed it
else
return false; // pass on to be processed as normal
}
});
I am now using the DialogFragment
class for my dialogs. Unfortunately DialogFragment
has the same problem as Dialog
in that dialogs are dismissed when the phone's search key is pressed (regardless of the dialog's cancelable state). Doubly problematic is that DialogFragment
does not have a setOnKeyListener
method so the above code segment no longer applies.
Anyone know how I can get my dialogs (when showing) to ignore search key presses?
Note: I tried adding the above code segment to my activity's
onKeyUp
method but unfortunately my activity'sonKeyUp
method is not called if aDialogFragment
instance is showing. Instead, irritatingly, theDialogFragment
instance is dismissed.
Try this.
Inside your DialogFragment.Onresume
getDialog().setOnKeyListener(new OnKeyListener()
{
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_SEARCH)
return true; // pretend we've processed it
else
return false; // pass on to be processed as normal
}
});
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