Android - 使用 AsyncTasks 的正确方法?
使用异步任务的正确方法是什么? 假设用户进入一个活动,asynctask 启动,当 asynctask 运行时,用户决定导航到另一个页面。
- anynctask 会发生什么?它会一直运行到完成吗?
- 我是否有责任以某种方式停止这个异步任务?
What is the proper way of using asynctasks?
Let's say a user enters an activity, and asynctask starts, and while the asynctask is running the user decides to navigate to another page.
- What's gonna happen to anynctask? is it gonna run until it's done?
- Am I responsible for stopping this asynctask somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
异步任务将继续运行。当 Android 操作系统在内存不足时尝试回收内存时,异步任务可能会终止。如果您不关心异步任务可能无法完成,那么也许这并不重要,但这是不好的做法。
另外,也许当异步任务终止时,具体取决于您存储/处理结果的方式,您可能会尝试编辑该活动中不再存在的活动/对象,从而导致空指针异常和其他不好的事情。执行此操作的正确方法是从您的活动启动服务并从该服务启动异步任务。您应该遵循 Google IO 2010 演讲中概述的设计模式之一:
Android REST 客户端设计模式
说得很有趣,但很难理解,而且没有提供代码,所以这里有一个很好的教程,我用来学习这些东西:
使用服务下载数据
另外,android 将管理为您提供异步任务的线程池(我相信当您的进程中有超过 5 个任务时它会开始杀死它们),您不必杀死它们。在此处查看答案:
AsyncTasks 如何死亡
The asynctask will keep running. The asynctask might die as the Android OS tries to reclaim memory when it's running low. If you don't care that the async task might not finish, then maybe this doesn't matter, but it's bad practice.
Also, maybe when the async task dies depending on how you're storing/handling the result, you risk trying to edit an Activity/objects in that activity that are no longer there, resulting in nullpointerexceptions and other bad things. The proper way of doing this would be to start a service from your activity and start an asynctask from the service. You should follow one of the design patterns outlined in this talk given at Google IO 2010:
Android REST Client Design Patterns
That talk interesting but is hard to follow and gives no code, so here's a good tutorial that I used to learn this stuff:
Downloading Data with Services
Also, android will manage a threadpool of your asynch tasks for you (i believe it will start killing them when you have more than 5 in your process), you don't have to kill them. check out the answer here:
How AsyncTasks die
好吧,我会尝试更快:
如果不对您的程序产生负面影响,您必须负责停止异步任务。阅读本文。
您的异步任务将通过 onPreExecute()-doInBackground(Params...)-onPostExecute(Result)-异步任务生命周期结束来度过其生命周期
Well ill try to be faster:
You must be responsible to stop an asynctask if not doing influence negatively your program . Read this.
Your asynctask would pass its life doing onPreExecute()-doInBackground(Params...)-onPostExecute(Result)-End of asynctask life
是的,如果您在导航到另一个活动时没有停止它,AsyncTask 将继续运行。即使设备的配置发生变化(例如旋转),它也会继续运行。如果您想在 postExecute() 上执行一些对 UI 产生影响的操作,那么您必须确保您在此方法中修改的 UI 小部件的引用是有效的,即使在旋转之后、重新创建 Activity 时也是如此。
是的,如果您不需要它,您有责任停止(这意味着,.cancel())您的 AsyncTask。但请注意,正在执行的 AsyncTask 不会仅因为您对其调用 .cancel() 而停止。它将继续运行,但任务完成时不会调用其 postExecute() 方法。
Yes, the AsyncTask will continue to run if you didn't stop it upon navigation to another Activity. It will continue to run even if the device's configuration changes (e.g. it rotates). If you want to do something on postExecute() that has an effect on the UI, then you have to make sure, that the references of the UI widgets that you modify in this method are valid, even after rotation, when the Activity's recreated.
Yes, you're responsible to stop (that means, .cancel()) you AsyncTask if you don't need it. Note however, that an AsyncTask that is in the middle of its execution will not stop only because you called .cancel() on it. It will continue to run, but its postExecute() method will not be called when the task is completed.