Prevent ProgressDialog from being dismissed when I click the search button (Android)

In a long-running operation, I'm showing a popup dialog (created from ProgressDialog to prevent other operations from happening).

I have made it non-cancellable with setCancelable(false), so I can't close it using the back button, but surprisingly, the Search hardware button dismisses the dialog!

More exactly, the global search application is displayed, and when I come back to my app, the dialog has been dismissed.

Any idea how to prevent the dialog from being dismissed?


This works (notice I put it on the dialog builder):

.setOnKeyListener(new DialogInterface.OnKeyListener() {

    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
            return true; // Pretend we processed it
        return false; // Any other keys are still processed as normal

Maybe it's even possible to grab the positive and negative button presses, and only handle these, return true for any other keys. Would be curious if you can figure that out...

PS: I read somewhere there are more "holes" in the dialog, i.e you can get rid of it without hitting any buttons on it. This was apparently one. Does anybody know of any others?


mProgressDialog.setCancelable(false); worked for me.

You have to create your dialogs via the Activity's onCreateDialog event, as that is the point where they are managed by the Activity and they will be restored when the activity resumes.

Callback for creating dialogs that are managed (saved and restored) for you by the activity. If you use showDialog(int), the activity will call through to this method the first time, and hang onto it thereafter. Any dialog that is created by this method will automatically be saved and restored for you, including whether it is showing. If you would like the activity to manage the saving and restoring dialogs for you, you should override this method and handle any ids that are passed to showDialog(int). If you would like an opportunity to prepare your dialog before it is shown, override onPrepareDialog(int, Dialog).

Example usage:

public class MyClass extends Activity {
// ........ //
static final int DATE_DIALOG_ID = 1;
 protected Dialog onCreateDialog(int id) {
  switch (id) {

   return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
  return null;

    public void launchSetDate() {

Need Your Help

DIV with max-height and overflow-y causes IE11 to enable window's vertical scroll-bar --- why?

css html overflow internet-explorer-11

As it would be hard to include whole code here, there's a brief description of the problem (and some crafted snippets later): there's this DIV element with display set to none which popups by setting

Mockito GregorianCalendate.getTime() causing error

java junit mockito

I am trying to mock the return of GregorianCalendar.getTime() which should be a Date().