Android AsyncTask Context Terminated

橙三吉。 提交于 2019-12-11 22:26:29

问题


When an Activity terminates, e.g. after screen orientation changing, is that possible to change an AsyncTask activity context? Else it will create an error because when the activity terminates AsyncTask's activity context is gone too.

My homework done is the following:

public void onSaveInstanceState(Bundle savedInstanceState) <- doesn't solve
public Object onRetainNonConfigurationInstance()  <- doesn't solve
android:configChanges="keyboardHidden|orientation" 
                     <- solved but doesn't handle well relative layouts

回答1:


What do you pass on your onRetainNonConfigurationInstance()? What I do is pass an object to it containing the AsyncTask, and then I try to retrieve the value in getLastNonConfigurationInstance().

EDIT: On second thought, it would depend on what you want to do after a configuration change. If you want to terminate the AsyncTask, and then call cancel() on it. If you want to continue its processing even after an orientation change, then you have to hold on to the task.

You can do that by saving the Activity in the AsyncTask like this:

private MyAsyncTask searchTask;

@Override
public void onCreate(Bundle savedInstance){
 super.onCreate(savedInstance);

 if (getLastNonConfigurationInstance()!=null) {
  SavedObject savedObj = (SavedObject)getLastNonConfigurationInstance();

  searchTask = savedObj.getAsyncTask();
  searchTask.attach(this);
 } else {
  searchTask = new MyAsyncTask(this);
  searchTask.execute();
 }
}

@Override
public Object onRetainNonConfigurationInstance(){

 searchTask.detach();

 final SavedObject savedObj = new SavedObject();
 savedObj.setAsyncTask(searchTask);

    return savedObj;
}


private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

 MyActivity parentActivity = null;

    MyAsyncTask (MyActivity activity) {
  attach(activity);   
 }

 void attach(MyActivity activity) {
  this.parentActivity=activity;
 }

 void detach() {
  parentActivity=null;
 }

 // Do your thread processing here
}


private class SavedObject {
 private MyAsyncTask asyncTask;

 public void setAsyncTask(MyAsyncTask asyncTask){
  this.asyncTask = asyncTask;
 }

 public MyAsyncTask getAsyncTask() {
  return asyncTask;
 }
}



回答2:


in the OnCancel method of your asynch task put finish();

        public void onCancel(DialogInterface dialog) {
        cancel(true);
        dialog.dismiss();
        finish();
    }


来源:https://stackoverflow.com/questions/4404993/android-asynctask-context-terminated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!