当 Activity 发生变化时,运行 AsyncTasks 会发生什么? [英] What happens to running AsyncTasks when the Activity changes?

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

问题描述

当网络操作在 Asynctask 中运行时,如果用户按下后退按钮并切换到另一个活动,在后台运行的 Asynctask 会发生什么?

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 进程会被操作系统自动杀死吗?

  1. AsyncTask Process automatically Kill by OS?

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

Async Task complete it's entire operation?

推荐答案

AsyncTask 是一个抽象的 Android 类,它帮助 Android 应用程序以有效的方式处理主 UI 线程.AsyncTask 类允许我们在不影响主线程的情况下执行持久任务/后台操作并在 UI 线程上显示结果.

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 进程不会被操作系统自动终止.AsyncTask 进程在后台运行,并且在任何情况下都负责完成自己的工作.您可以通过调用 cancel(true) 方法取消您的 AsycnTask.这将导致对 isCancelled() 的后续调用返回 true.调用此方法后,在 doInBackground() 返回后调用 onCancelled(Object) 方法而不是 onPostExecute().

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.它的操作完成后,正在运行的后台线程停止.AsyncTask 有一个 onPostExecute(),它会在您的工作完成后调用.在doInBackground() 方法完成处理后调用此方法.doInBackground() 的结果被传递给这个方法.

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.

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

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