I ran into these issues too and the only solution I found so far that works 
in any case (rotation, home button & restart activity, sliding out keyboard, 
killing an app etc.) is using an AsyncTask.
I'm not a big fan of AsyncTasks normally but to show and dismiss a 
ProgressDialog this seems to be a fair solution:

private ProgressDialog progressDialog;

AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MyActivity.this);
// some progressDialog setter calls 
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// do your background stuff
}
@Override
protected void onPostExecute(Void result) {
dismissProgressDialog();
}
}.execute();

private void dismissProgressDialog() {
try {
progressDialog.dismiss();
}
catch (Exception e) {}
}

@Override
protected void onDestroy() {
super.onDestroy();
dismissProgressDialog();
}

No fiddling around with static variables which could cause problems in a 
multithreaded environment, no worries about the activity's lifecycle, no 
worries about configuration changes.
Honestly I don't understand why showing and dismissing a ProgressDialog is 
such a hassle. I would expect the framework do handle this for us 
transparently.
And I don't think it's the price we have to pay for having a real 
multithreaded environment.

Of course it get's slightly more difficult if the task can be cancelled or 
if you want to keep the background task running and show the progress dialog 
whenever the ui is shown.
For long lasting tasks like initializing a large database upon first start 
of an app or a file download I recommend using a Service and using the 
activity as gui to show the Service's progress.
I elaborated on that solution in this thread: http://bit.ly/qim7ov (last 
post).

Emanuel Moecklin
1gravity LLC

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to