AsyncTask chaining in Activity (callback?)

为君一笑 提交于 2020-01-16 19:17:27

问题


I am trying to chain a few AsyncTask classes inside a single Activity, which will then show another Activity once they are all completed (successfully). Currently I am chaining them off each others' onPostExecute() method but I do not like this because it somewhat couples the implementations unnecessarily.

For example, I have my ImportVideoActivity which the users enters a YouTube URL to be downloaded. To do so, I have two AsyncTask classes:

  • YouTubeVideoInfoTask : Gets the video metadata info as well as enumerating through available video formats (quality and codecs).
  • YouTubeVideoDownloadTask : Performs the video download given a URL from the video info metadata.

I want the ImportVideoActivity to first execute a YouTubeVideoInfoTask to enumerate the video qualities and pick the best one. After that, it should execute a YouTubeVideoDownloadTask and upon completion start the CropVideoActivity with the downloaded video.

How can I chain these two AsyncTask classes elegantly inside of the ImportVideoActivity? Is there some kind of listener/callback that allows me to monitor when these tasks complete and start the next one without using the AsyncTask.onPostExecute() method?


回答1:


Yes. Use a Callback Listener in your Activity. As soon as this is called, you can start the new AsyncTask from your Activity.

Example in my other answer: How to return an object from the asynctask to the main class in android



来源:https://stackoverflow.com/questions/24194651/asynctask-chaining-in-activity-callback

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