views:

389

answers:

4

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 setCancellable(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?

A: 

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);
 }
}
Pentium10
I've tried that, and the dialog was still dismissed when I press the search button.
yuku
And when you came back to the application?
Pentium10
@Pentium10 the progress dialog is no more there :(
yuku
+1  A: 

What about overriding the search button function?

@Override
public boolean onSearchRequested() {
    return true;
}
janfsd
+1  A: 

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?

QQQuestions
A: 

Hi

I am having trouble implimenting this solution. I see you say you put the onKeyListener on the dialog builder, how did you do this? I cannot see how to do this with a progress dialog. I only see how to use the dialog builder with the alert dialogs.

So how do i get from here: gps_progress_dialog = ProgressDialog.show(Christmas_Light_Controller.this, "GPS Status", "Waiting for Position.", true,true); to an implimentation where I can have the onKeyListener event as you show.

Thanks Lucas

Lucas