I have seen the previous answers, speaking about dismissing the dialog in onDestoy()
and onPostExecute
.
1/ There is a problem : The fact onDestoy()
and onPostExecute
are not always lunched ! So anyway , you can have this problem of crash ...
->It would be better to dismiss your dialog in onPause()
and in the end of doInBackground()
.
The fact is that everytime that your orientation changes, a new View
is created. Then you can make your activity handle better this by adding the onConfigChanges
propertiy in your XML:
<activity ...
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
So to conclude, you define your ProgressDialog
as global property in your activity, then you are sure that it's part of the activity Context, and you can update from everywhere in your activity ... You add a methode that dismiss the progress with checking it :
/**
* Instance of ProgressDialog.
*/
private ProgressDialog dialog;
you can instantiate and it somewhere in your code like this :
dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.setCancelable(true);
// show the loading dialog
dialog.show();
now the dismiss methode:
/**
* check that the dialog exists before dismissing it.
*/
private void dialogDismiss() {
try {
if (Util.isNotEmpty(dialog)) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
} catch (Exception e) {
Log.e(TAG, "problem with the dialog dismiss again !!!", e);
}
}
then from everywhere in this activity, you call dialogDismiss();
and your dialog is dismissed !
Kind regards,
good luck !