views:

20

answers:

1

In the onCreate of an activity we are connecting to a remote system and downloading data. We display a cancellable ProgressDialog to the user all this time. Download is done using AsyncTask. In preExecute(), we show the dialog and in postExecute() we dismiss it.

Problem is that when the download is in progress and ProgressDialog on display, if the user presses the "search" button on the device, the ProgressDialog disappears. The background thread is still running.

What cause this behavior? How can this be avoided?

Please help.

thanks.

A: 

This should fix that 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 there are more "holes" in the dialog, i.e you can get rid of it without hitting any buttons on the dialog itself. This was apparently one. Does anybody know of any others?

PS2: this seems a repeat of http://stackoverflow.com/questions/2502443/prevent-progressdialog-from-being-dismissed-when-i-click-the-search-button-andro

QQQuestions
I thought there would be a short cut (like some property) of achieving this. Anyways, many thanks!
Samuh