Android-How to close progress dialog when back button is pressed
How to close progress dialog when back button开发者_StackOverflow社区 is pressed ?
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).
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();
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.
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
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
.
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.
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();
}
Here is one possible solution:
Dialog dialog;
dialog = new Dialog(context,style);
dialog.setCanceledOnTouchOutside(false);
ProgressDialog dialog = new ProgressDialog(yourActivity.this);
dialog.setCancelable(true);
.
.
.
progressDialog.show();
Hope, this will work.
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);
精彩评论