What is the best way for AsyncTask to notify parent Activity about completion?

后端 未结 2 822
-上瘾入骨i
-上瘾入骨i 2020-12-15 13:36
public class ListingFoundBeaconService 
                    extends AsyncTask {

    public ListingFoundBeaconService(Context contextGi         


        
相关标签:
2条回答
  • 2020-12-15 14:06

    Best way is to call delegate. In your AsyncTask class make a constructor and have a delegate

    Delegate interface:

    public interface TaskDelegate {
        public void taskCompletionResult(String result);
    }
    

    now in AsyncTask:

    private TaskDelegate delegate;
    
    public ListingFoundBeaconService(Context contextGiven, 
                                     JSONObject jsonParams,
                                     TaskDelegate delegate) {
        this.contextGiven = contextGiven;
        this.jsonParams = jsonParams;
        this.delegate = delegate;
    }
    

    on postExecute:

    delegate.taskCompletionResult(result/msg/json);
    

    In your main class implement TaskDelegate and implemented a method which is called when the task completed.

    0 讨论(0)
  • 2020-12-15 14:06

    Make the ListingFoundBeaconService class abstract and then override onPostExecute in the calling class.

    0 讨论(0)
提交回复
热议问题