Reputation: 505
How to close progress dialog when back button is pressed ?
Upvotes: 9
Views: 22659
Reputation: 1
ProgressDialog dialog = new ProgressDialog(yourActivity.this);
dialog.setCancelable(true);
.
.
.
progressDialog.show();
works very fine
Upvotes: 0
Reputation: 340
You can dismiss dialog in onPause method of activity. Here is the solution. It worked in case of me.
@Override
public void onPause() {
super.onPause();
progressDialog.dismiss();
}
Make sure private ProgressDialog progressDialog;
declared globally
and initialize progressDialog
on onCreate
method just like this:
progressDialog = new ProgressDialog(YourActivityGoesHere.this);
Upvotes: 0
Reputation: 542
ProgressDialog dialog = new ProgressDialog(yourActivity.this);
dialog.setCancelable(true);
.
.
.
progressDialog.show();
Hope, this will work.
Upvotes: 0
Reputation: 369
By default progress dialog
get dismiss
, if you click on back button
. Or, you can add this line:
progress.setCancelable(true);
Another option is you can call finish()
and then progress.dismiss()
:
progress.setOnKeyListener(new ProgressDialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Or, you override
method onCancel()
method in back key
pressed button event
.
Upvotes: 2
Reputation: 36035
A much better way.
ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog){
/****cleanup code****/
}});
The setCancelable
method tells the ProgressDialog to close when the back button is pressed. The listener at the end allows you to do anything that may need to be done as a result of a cancel (like closing a socket).
Upvotes: 46
Reputation: 1
Here is one possible solution:
Dialog dialog;
dialog = new Dialog(context,style);
dialog.setCanceledOnTouchOutside(false);
Upvotes: 0
Reputation: 19
It's very simple just copy the below code and paste within Async..
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this) {
@Override
public void onBackPressed() {
dialog.cancel();
dialog.dismiss();
}
};
// dialog.setTitle("file is..");//its optional If u want set title in progress
// bar
dialog.setMessage("Loading....");
dialog.setCancelable(false);
dialog.show();
}
Upvotes: 0
Reputation: 33
I would honestly just do:
The Dialog interface provides a dialog.setCanceledOnTouchOutside(false) method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity
Upvotes: 3
Reputation: 351
Well i found my approach much more useful. if you set progressDialog.setCancelable(true); it will cancel the dialog if the user would press anywhere on the screen outside the dialog. You wouldn't want that, right? If you want the progress dialog to cancel only if the user presses the back button then use this code:
ProgressDialog pDialog;
pDialog = new ProgressDialog(MainActivity.this) {
@Override
public void onBackPressed() {
pDialog.dismiss();
}};
pDialog.setMessage("Loading");
pDialog.setCancelable(false);
pDialog.show();
Upvotes: 5
Reputation: 1449
ProgressDialog progressDialog = ProgressDialog.show(ActivityName.this,
"Title","Message");
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
// Do something...
}
});
When you press the back button, onCancel
is called.
Upvotes: 1
Reputation: 3226
Here is the solution.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
return true;
}
return super.onKeyDown(keyCode, event);
}
or also you can dismiss dialog in onPause method of activity.
Upvotes: 4