michael
michael

Reputation: 110540

How to handle the 'back button' on android when I have a progress dialog pops up

When my android activity pops up a progress dialog, what is the right way to handle when user clicks the back button?

Thank you.

Upvotes: 5

Views: 13872

Answers (4)

progressbar.setcancellable(false);

It works perfectly, I was facing same problem and got the solution.

Upvotes: 0

Bghaak
Bghaak

Reputation: 1170

You can use the static call:

public static ProgressDialog show (Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, DialogInterface.OnCancelListener cancelListener) 

Just be sure to set cancelable = true and to add the appropriate cancelListener callback.

Example:

progressDialog = ProgressDialog.show(this, "Fetching data...", "Loading ...", true, true, this);

Upvotes: 0

chich
chich

Reputation: 411

if your not having any luck with the onCancel() method, than you can either write up a onKeyDown() method in your activity to check if the progress dialog is showing and if the back button is pressed...or you can override the onBackPressed() method (again, for the activity) and check if your dialog is showing. alternatively you can extend the ProgressDialog class and override onBackPressed() directly from there...in which case you wouldnt have to check if the dialog is showing.

eg. for activity method:

public void onBackPressed()
{
    if (progressDialog.isShowing())
    {
        // DO SOMETHING
    }
}

the onKeyDown() method would be similar, however you'd have to check whether the dialog is showing, whether the button being pressed is the 'back' button, and you should also make a call to super.onKeyDown() to make sure the default method is also executed.

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK && progressDialog.isShowing())
    {
        // DO SOMETHING
    }

    // Call super code so we dont limit default interaction
    super.onKeyDown(keyCode, event);

    return true;
}

with either method, the progressDialog variable/property would obviously have to be in scope and accessible.

Upvotes: 6

AndrewKS
AndrewKS

Reputation: 3837

Make a new dialog object that extends ProgressDialog, then override the public void onBackPressed() method within it.

Upvotes: 12

Related Questions