恰好运行AsyncTasks活动发生变化时怎么办? [英] What happens to running AsyncTasks when the Activity changes?

查看:202
本文介绍了恰好运行AsyncTasks活动发生变化时怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络运行中的AsyncTask运行,如果用户preSS后退按钮并切换到另一个活动将发生在这是在后台运行的AsyncTask是什么?


  1. AsyncTask的流程自动操作系统杀人?


  2. 异步任务完成它的整个操作?



解决方案

的AsyncTask 是一个抽象的Andr​​oid类这有助于Android应用程序来处理有效的方式在主UI线程。 AsyncTask的类允许我们在不影响主线程UI线程上执行持久的任务/后台操作和显示结果。

1 AsyncTask的过程是的不会自动OS杀死的。 AsyncTask的进程在后台运行和负责完成它在任何情况下,自己的工作。您可以通过调用cancel(true)方法取消AsycnTask。这将导致isCancelled()的后续调用返回true。调用此方法后,onCancelled(Object)方法被调用,而不是onPostExecute()doInBackground()后的回报。

2 完成它的操作后,在后台线程它的工作就停止。 AsyncTask的拥有的 onPostExecute()的,一旦你的工作完成时调用。的 doInBackground()的方法完成加工后该方法被调用。从结果的 doInBackground()的传递给这个方法。

While Network Operation is running in Asynctask, If user press the Back button and switch to another activity what will happen to Asynctask which is running in background?

  1. AsyncTask Process automatically Kill by OS?

  2. Async Task complete it's entire operation?

解决方案

AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in efficient way. AsyncTask class allows us to perform long lasting tasks/background operations and show the result on the UI thread without affecting the main thread.

1. AsyncTask processes are not automatically killed by the OS. AsyncTask processes run in the background and is responsible for finishing it's own job in any case. You can cancel your AsycnTask by calling cancel(true) method. This will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object) method is called instead of onPostExecute() after doInBackground() returns.

2. After completion of it's operation, the background thread it's working on is stopped. AsyncTask has an onPostExecute() which is called once your work is finished. This method is called after doInBackground() method completes processing. Result from doInBackground() is passed to this method.

这篇关于恰好运行AsyncTasks活动发生变化时怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆