Попробуйте этот небольшой хак, который я собрал... запустите его и наблюдайте за информацией logcat, когда вы пробуете различные комбинации изменений экрана и нажатия аппаратной клавиши возврата. Затем попробуйте настройку, упомянутую в комментариях ниже, и поработайте еще немного. Это позволяет избежать использования android:configChanges="orientation|screenSize", что нежелательно.
Это должно быть собрано как есть с минимальным манифестом и файлом layout.xml. Оставляйте комментарии, если я как-то накосячил.
// ********************************************************************
// *** AsynchHack - allows for multiple screen orientation changes, ***
// *** multiple asynch tasks are started / stopped,progress ***
// *** dialogs handled gracefully (no memory leaks / etc). ***
// *** ***
// *** Remove the wrapping comments in the onCreate() function ***
// *** to restrict processing to a single asynch instantiation ***
// *** screen rotation will dismiss the progress meter, but not ***
// *** the task ***
// *** ***
// *** View the logcat to understand the timing of events and ***
// *** their interactions ***
// *** ***
// ********************************************************************
package com.example.AsynchHack;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
// *** Main activity
public class AsynchHack extends Activity {
final static String debugClass = "AsynchHack";
ProgressDialog mProgressDialog;
// *** standard onCreate()
public void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
Log.d(debugClass, "onCreate(" + this + ")");
setContentView(R.layout.layout);
// As-is, this allows for multiple starts of asynch tasks
//if (aSavedInstanceState == null) {
mProgressDialog = new ProgressDialog(this);
(new AsynchStartup()).execute();
//}
}
// *** demo Asynch task
private class AsynchStartup extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Log.d(debugClass,
"--- AsynchStartup() onPreExecute (" + this + ")");
mProgressDialog.setMessage("Loading please wait..");
mProgressDialog.show();
}
protected Void doInBackground(Void... aInput) {
Log.d(debugClass,
"--- AsynchStartup() doInBackground(" +
this + ") *starts*");
// simulate lengthy processing
try {
Thread.sleep(5000);
}
catch (InterruptedException aException) { }
Log.d(debugClass,
"--- AsynchStartup() doInBackground(" +
this + ") *finishes*");
return null;
}
protected void onProgressUpdate(Void aProgress){
}
protected void onPostExecute(Void aOutput) {
Log.d(debugClass,
"--- AsynchStartup() onPostExecute (" + this + ")");
if (mProgressDialog != null) {
Log.d(debugClass,
"--- AsynchStartup() onPostExecute " +
"(Dismissing progress meter)");
mProgressDialog.dismiss();
mProgressDialog = null;
}
else {
Log.d(debugClass,
"--- AsynchStartup() onPostExecute " +
"(Not required to dismiss progress meter)");
}
}
}
// *** standard onDestroy()
public void onDestroy() {
super.onDestroy();
Log.d(debugClass, "onDestroy(" + this + ")");
if (mProgressDialog != null) {
Log.d(debugClass,
"onDestroy(Dismissing progress meter)");
mProgressDialog.dismiss();
mProgressDialog = null;
}
else {
Log.d(debugClass,
"onDestroy(Not required to dismiss progress meter)");
}
}
}
person
capella
schedule
09.12.2012