2

I have an application when I click a button then execute AsyncTask in that onPreExecute show ProgressDialog code and in onPostExecute i am using dialog.dismiss().

In my manifest file I declare android:screenOrientation="portrait" in Activity, but when I click button start ProgressDialog and when change screen orientation it crashes.

After searching, I got this link How to handle screen orientation change when progress dialog and background thread active?.

But, I can't understand what I'm supposed to do.

Community
  • 1
  • 1
kalpana c
  • 2,739
  • 3
  • 27
  • 47

3 Answers3

3

You can put a line android:screenOrientation="portrait" in your manifest or You can dismiss dialog in onDestoy() method of activity. for that you have to declare progressdialog globally and in onDestoy() check progress dialog

for example

if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog  = null;
}
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • 1
    if(progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); //progressDialog = null; } this is working for me so +1 for you thanks for reply – kalpana c Oct 12 '11 at 11:49
  • 1
    Please mark this question as solved any of the answer worked for you – Kartik Domadiya Oct 12 '11 at 11:51
1

This is because your Activity is trying to dismiss the dialog which has already lost its context.
android:configChanges="keyboardHidden|orientation" add this to your Activity in manifest.

Sujit
  • 10,512
  • 9
  • 40
  • 45
0

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 !

ahmed_khan_89
  • 2,755
  • 26
  • 49