且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android AsyncTask - 执行顺序

更新时间:2023-11-30 10:01:34

确保两个线程(这就是 AsyncTasks 基本上就是这样)按照你想要的顺序执行的唯一方法是在第一个线程时启动第二个线程结束.

The only way to ensure that two threads (that's what AsyncTasks basically are) are executed in the order you want, is to start the second thread when the first one finishes.

在您的情况下,为了保持实现抽象并且不必在 AsyncTask1 的 onPostExecute 中实际调用 AsyncTask2(Anup 和 Sanket 建议的方式,如果您想混合它们也可以),让 AsyncTask1 调用 super.executeAsyncTask2(),其中 executeAsyncTask2() 是 CustomActivity 中的一个方法,它启动第二个 AsyncTask

In your case, to keep implementation abstracted and not have to actually call AsyncTask2 in the onPostExecute of AsyncTask1 (the way Anup and Sanket suggested, which is also fine if you want to mix them), make AsyncTask1 call super.executeAsyncTask2(), where executeAsyncTask2() is a method in your CustomActivity which starts the second AsyncTask