在我的一个活动中,我确实有六个不同的AsyncTasks可以在不同的事件上运行。所有AsyncTasks都优雅地处理方向更改(至少我希望如此)。在onRetainNonConfigurationInstance()中,我确实返回当前运行的AsyncTask的AsyncTask对象。稍后,在onCreate()期间,我需要找出从getLastNonConfigurationInstance()返回的AsyncTask对象。
我在所有onPostExecute()方法中使用Activity上下文来获取新活动(如果有新活动)。我在StackOverflow上找到了这个概念并且稍微修改了它,因为我不喜欢“Trash running orientation on orientation change”paradigma。希望这个概念是正确的。
在下面显示的代码中,您将找到两个不同的AsyncTasks。在onRetainNonConfigurationInstance()期间,我将返回当前运行的AsyncTask。我的问题在onCreate()中。如何找出返回的对象?当方向变化碰到时,AsyncTask正在运行什么?
AsyncTasks在许多方面都有所不同(此处未显示),因此我没有使用自己的扩展AsyncTask基础对象。
非常感谢提前。
public class MyActivity extends ListActivity {
// First AsyncTask
private class MyLoadAsyncTask extends AsyncTask<Void, Void, Cursor> {
private MyActivity context;
private MyProgressDialog dialog;
public MyLoadAsyncTask(MyActivity context) {
super();
this.context = context;
}
@Override
protected Cursor doInBackground(Void... voids) {
return MyApplication.getSqliteOpenHelper().fetchSoomething();
}
@Override
protected void onPostExecute(Cursor cursor) {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
if (cursor != null) {
context.startManagingCursor(cursor);
context.adapter = new InternetradiosAdapter(context,
R.layout.row,
cursor,
new String[] { "text1",
"text2" },
new int[] { R.id.row_text1,
R.id.row_text2 } );
if (context.adapter != null) {
context.setListAdapter(context.adapter);
}
}
context.loadTask = null;
}
@Override
protected void onPreExecute () {
dialog = MyProgressDialog.show(context, null, null, true, false);
}
}
private class MyDeleteAsyncTask extends AsyncTask<Void, Void, Boolean> {
// Second AsyncTask
private MyActivity context;
private MyProgressDialog dialog;
private long id;
public MyDeleteAsyncTask(MyActivity context, long id) {
super();
this.context = context;
this.id = id;
}
@Override
protected Boolean doInBackground(Void... voids) {
return MyApplication.getSqliteOpenHelper().deleteSomething(id);
}
@Override
protected void onPostExecute(Boolean result) {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
if (result) {
context.doRefresh();
}
context.deleteTask = null;
}
@Override
protected void onPreExecute () {
dialog = MyProgressDialog.show(context, null, null, true, false);
}
}
private MyDeleteAsyncTask deleteTask;
private MyLoadAsyncTask loadTask;
@Override
public void onCreate(Bundle bundle) {
// ...
// What Task is returned by getLastNonConfigurationInstance()?
// ...
// xxxTask = (MyXxxAsyncTask) getLastNonConfigurationInstance();
// ...
if (deleteTask != null) {
deleteTask.context = this;
deleteTask.dialog = MyProgressDialog.show(this, null, null, true, false);
} else if (loadTask != null) {
loadTask.context = this;
loadTask.dialog = MyProgressDialog.show(this, null, null, true, false);
} else {
loadTask = new MyLoadAsyncTask(this);
loadTask.execute();
}
}
@Override
public Object onRetainNonConfigurationInstance() {
if (deleteTask != null) {
if (deleteTask.dialog != null) {
deleteTask.dialog.dismiss();
deleteTask.dialog = null;
return deleteTask;
}
} else if (loadTask != null) {
if (loadTask.dialog != null) {
loadTask.dialog.dismiss();
loadTask.dialog = null;
return loadTask;
}
return null;
}
}
答案 0 :(得分:1)
我发现处理在其中运行多个AsyncTasks的活动的最佳方法是实际返回包含所有正在运行的对象的对象,并自动重新初始化onCreate()
中的所有上下文。 E.g。
private class AsyncTaskList() {
List<ActivityTask> tasks; //interface all of your AsyncTasks implement
public void addTask() { /* add to list*/ }
public void completeTask { /* remove from list */ }
public void attachContext(Activity activity) {
for ( ActivityTask task : tasks) {
//You can also check the type here and do specific initialization for each AsyncTask
task.attachContext(activity);
}
}
}
public void onCreate(Bundle bundle) {
AsyncTaskList taskList = (AsyncTaskList) getLastNonConfigurationInstance();
if (taskList != null) {
taskList.attachContext(this);
}
...
}
现在您只需要在开始/结束时添加和删除任务。
答案 1 :(得分:1)
在你的onCreate()中添加:
Object savedInstance = getLastNonConfigurationInstance();
if(savedInstance instanceof MyDeleteAsyncTask){
//it's a MyDeleteAsyncTask
}else if(savedInstance instanceof MyLoadAsyncTask){
//it's a MyLoadAsyncTask
}