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

5

42

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?

Bess answered 23/3, 2010 at 18:10 Comment(2)
What about overriding the search button function? @Override public boolean onSearchRequested() { return true; }Kibitz
Surprisingly, this does not work (at least testing it on 2.3). Doesn't work if you return false either.Revareval
P
45

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

.setOnKeyListener(new DialogInterface.OnKeyListener() {

    @Override
    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?

Powerdive answered 19/9, 2010 at 14:30 Comment(3)
I get compiler errors when I run this, however removing @Override and changing onKey's declaration to public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) worked for me - Thanks!Roquefort
@MohamedHafez maybe change your compiler/IDE to check against Java 1.6 instead of 1.5?Eipper
@Powerdive any idea why the getRepeatCount() == 0 has to be there?Eipper
U
37
setCanceledOnTouchOutside(false);
Unblushing answered 5/9, 2013 at 20:5 Comment(0)
E
4

mProgressDialog.setCancelable(false); worked for me.

Exteriorize answered 7/2, 2016 at 20:45 Comment(0)
N
1

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;
@Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {

  case DATE_DIALOG_ID:
   return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
     mDay);
  }
  return null;
 }

    public void launchSetDate() {
  showDialog(DATE_DIALOG_ID);
 }
}
Nonpartisan answered 23/3, 2010 at 18:22 Comment(3)
I've tried that, and the dialog was still dismissed when I press the search button.Aixlachapelle
And when you came back to the application?Nonpartisan
the progress dialog is no more there :(Aixlachapelle
S
0

simply use this code

progressDialog.setMessage("please wait..."); progressDialog.setCancelable(false); progressDialog.show();

Stavros answered 9/11, 2022 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.