how do i send data back from onPostExecute in an AsyncTask?

前端 未结 2 532
清歌不尽
清歌不尽 2020-11-27 21:39

my issue is the same as this Instance variable of Activity not being set in onPostExecute of AsyncTask or how to return data from AsyncTask to main UI thread but i want to

2条回答
  •  天涯浪人
    2020-11-27 22:33

    On option is to use listeners, where you create an interface that your activity implents, something like:

    public interface AsyncListener {
        public void doStuff( MyObject obj );
    }
    

    That way, if you're subclassing AsyncTask, it is easy to add this listener, then in onPostExecute(), you could do something like:

    protected void onPostExecute( MyObject obj ) {
       asyncListener.doStuff(obj);
    }
    

提交回复
热议问题