2
使用AsyncTask从活动执行一些重要的后台工作时,通过标志FLAG_ACTIVITY_CLEAR_TOP启动活动时。后台任务会发生什么?使用FLAG_ACTIVITY_CLEAR_TOP启动活动
使用AsyncTask从活动执行一些重要的后台工作时,通过标志FLAG_ACTIVITY_CLEAR_TOP启动活动时。后台任务会发生什么?使用FLAG_ACTIVITY_CLEAR_TOP启动活动
后台任务将继续处理。
从文档:
如果设置,以及正在启动的活动在目前的任务已经运行,然后,而不是在它的上面推出该活动的一个新实例,其他所有活动将被关闭,并且这个意图将作为新的意图被传递到(现在在上面的)旧活动。
如何工作AsyncTask? 让我们来看看的AsyncTask来源:
/**
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*/
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//noinspection unchecked
Result result = doInBackground(mParams);
Binder.flushPendingCommands();
return postResult(result);
}
};
mFuture = new FutureTask<Result>(mWorker) {
@Override
protected void done() {
try {
postResultIfNotInvoked(get());
} catch (InterruptedException e) {
android.util.Log.w(LOG_TAG, e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occurred while executing doInBackground()",
e.getCause());
} catch (CancellationException e) {
postResultIfNotInvoked(null);
}
}
};
}
我们只需要两件事情,需要被记住。
所以这里是您的答案。 doInBackground
将被处理,但onPostExecute
可能会产生NPE,因为父母的活动已被破坏。