My code tries to do this:
- onCreate it creates and instance of an asynctask A
- onPreExecute of A we make call: showDialog
- in doInBackground we sleep for a while SystemClock.sleep(1000)
- onPostExecute we dismiss the dialog then start a new Async Task B
- Task B does mostly the same by opening a dialog waiting then closing it.
Now when you first open the app things happen as the should: you see two Progress dialogs pop up one after the other, each for a period of time.
However if I rotate the screen at anytime, the next time the activity is created the progress dialogs are created but never dismissed. which makes it impossible to use the UI. If I rotate the screen again, I get an error message about leaked views (the dialogs that dint go away). plus new dialogs are now created and also don't want to go away (be dismissed).
here my code:
public class MainActivity extends ListActivity {
private static final int REFRESH_DIALOG = 100;
private Resources resources;
@Override
protected void onCreate(Bundle savedInstanceState) {
resources = getResources();
setContentView(R.layout.listview);
new CreationWork(this).execute();
super.onCreate(savedInstanceState);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case REFRESH_DIALOG:
String message = resources.getString(R.string.refreshing);
ProgressDialog refreshDialog = new ProgressDialog(this);
refreshDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
refreshDialog.setMessage(message + "...");
refreshDialog.setCancelable(false);
return refreshDialog;
default:
return null;
}
}
private static class CreationWork extends AsyncTask<Void, Void, DatabaseManager> {
private MainActivity activity;
public CreationWork(MainActivity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
activity.showDialog(MainActivity.REFRESH_DIALOG);
super.onPreExecute();
}
@Override
protected DatabaseManager doInBackground(Void... params) {
SystemClock.sleep(10000);
return null;
}
@Override
protected void onPostExecute(DatabaseManager result) {
activity.dismissDialog(MainActivity.REFRESH_DIALOG);
new RefreshWork(activity).execute();
super.onPostExecute(result);
}
}
static class RefreshWork extends AsyncTask<Void, Void, Cursor> {
private MainActivity activity;
public RefreshWork(MainActivity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
activity.showDialog(MainActivity.REFRESH_DIALOG);
super.onPreExecute();
}
@Override
protected Cursor doInBackground(Void... params) {
SystemClock.sleep(1000);
return null;
}
@Override
protected void onPostExecute(Cursor result) {
activity.dismissDialog(MainActivity.REFRESH_DIALOG);
super.onPostExecute(result);
}
}
}
Some debug info:
There are no errors untill a second rotation. which makes sense beacuse if the progress dialogs aren't dismissed between the first and second rotations, they are effectively leaked.
when the app first runs, despite two calls to showDialog, onCreateDialog is called only once (I believe this is the normal behaviour). however after the first rotation and for any subsequent rotation onCreateDalog will be called twice per run.