How to call parent activity function from ASyncTask?

懵懂的女人 提交于 2019-11-26 20:24:06

问题


setAccountAuthenticatorResult can be called from the Activity, which extends AccountAuthenticatorActivity. My activity extends that, but launches ASyncTask and hence this setAccountAuthenticatorResult should be called from ASyncTask (or, the result of ASyncTask should be passed back to the main thread).

How to do it?

What is wrong in the code below?

AsyncTask<Uri, Void, Bundle> task = new RetrieveAccessTokenTask(this, consumer, provider, prefs).execute(uri);

public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Bundle> {
    private Context context;

    public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,
            OAuthProvider provider, SharedPreferences prefs) {
        this.context = context;
    }

    @Override
    protected void onPostExecute(Bundle result) {
        context.setAccountAuthenticatorResult(); // doesn't work

    }

回答1:


When you create the AsyncTask, you can add a new constructor to it, and pass in a reference to the Activity:

AsyncTask myTask = new MyTask(this);

And then from the onPostExecute() method in the AsyncTask you can call the method on the Activity.

public class MyTask extends AsyncTask<String, String, String>
{
    public MyActivity activity;

    public MyTask(MyActivity a)
    {
        this.activity = a;
    }

    //  ......

    protected void onPostExecute(String result)
    {
        activity.myMethod();
    }
}



回答2:


Use Interface

Follow these steps:

1) Create an interface

public interface AsyncTaskListener{

   public void updateResult(String result);

}

2) Use the listener in your AsyncTask

DownloadSongTask extends AsyncTask<String,Integer,String>{

   private AsyncTaskListener listener;

   public DownloadSongTask(Context context)
   {
       listener= (AsyncTaskListener)context;    // Typecast 
   }

   @Override
   public void doInbackGround(String... params)
   {
       // Download code
       int downloadPerc = // calculate that
       publish(downloadPerc);   
   }

   @Override
   public void onPostExecute(String result)
   {
       listener.updateResult(String result);  // Use it 
   }

}

3) Implement the interface in your Activity and Override the interface method

  public class YourActivity extends AppcompatActivity implements AsyncTaskListener{

  // Activity code //
  new  DownloadSongTask(this).execute("Paradise.mp3");  // this is how you start Task

  public void yourMethod(String arg)
  {
    // Your method related Stuff
  }

  @Override 
  public void updateResult(String result){
        yourMethod(result);
  }

}

Advantege of using interface?

This seems a lengthy approach at first but if you use this approach

You can make a loosely coupled AsyncTask. Which means you can use same AsyncTask with any Activity in Future without even changing code in your AsyncTask.

Relevant Links:

For better understanding you can read this ANSWER



来源:https://stackoverflow.com/questions/5523438/how-to-call-parent-activity-function-from-asynctask

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