How to call parent activity function from ASyncTask?

后端 未结 2 1039
自闭症患者
自闭症患者 2020-12-05 15:04

setAccountAuthenticatorResult can be called from the Activity, which extends AccountAuthenticatorActivity. My activity extends that, but launches A

2条回答
  •  醉梦人生
    2020-12-05 15:17

    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
    {
        public MyActivity activity;
    
        public MyTask(MyActivity a)
        {
            this.activity = a;
        }
    
        //  ......
    
        protected void onPostExecute(String result)
        {
            activity.myMethod();
        }
    }
    

提交回复
热议问题