3

We created an android library which is working with dialogs. After testing it on a Samsung Galaxy S1 Plus I9005 with Android 2.3.6 we experienced, that a long press on the devices menu button closes the active dialog immediately. I do not even know the purpose of long-pressing that button. For someone who does not know the device, this picture should show you what i mean: Samsung Galaxy S1

The problem does not occur on devices running on Android 4.x. I tested it on a Samsung Galaxy S3 running Android 4.1.2 and even a Galaxy S1 with a Android 4.0.3 Custom ROM - no problem.

To suppress closing the dialog, i tried to override the onKeyDown, onKeyUp, onPrepareOptionsMenu and other methods which seemed to be relevant, but non of them brought success or any valuable information. The same happened when i tried to debug into those methods, because the dialog was gone before the debugger reached any method i can override.

The dialogs are created by using the following way:

@Override
public Dialog onCreateDialog(final int pId) {
    Dialog dialog;
    switch (pId) {
        case LOADING_DIALOG:
            dialog = DialogCreator.createLoadingDialog(MyActivity.this));
            break;
        default:
            dialog = null;
    }
    super.onCreateDialog(pId);
    return dialog;
}

The DialogCreator is just creating an AlertDialog by using:

new AlertDialog.Builder(pContext).create();

To prevent the dialog from being canceled I added:

alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);

Anybody an idea why this is closing the dialog?

edit

While being on the 'Desktop' the long press on the menu button starts a Google search.

1 Answers1

3

The answer of @QQQestions has helped me solve this problem. You should use OnKeyListener to intercept menu button long click event.

EDIT

if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
 return true; // Pretend we processed it
} else if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
 return true; // Pretend we processed it
}
Community
  • 1
  • 1
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
  • @DanielVermaasen On different vendors it may have different keys event try to catch them via debug and then handle them in your code. A have add code to ansver. – Roman Nazarevych Jul 23 '13 at 08:01
  • I have face same issue and get solved by your code for Samsung Galaxy Y android 2.3.6 OS. – Herry Sep 27 '13 at 10:29