gpt4 book ai didi

android - 使用 FLAG_ACTIVITY_CLEAR_TOP 启动 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 09:47:14 25 4
gpt4 key购买 nike

当使用 AsyncTask 执行一些重要后台工作的 Activity 使用标志 FLAG_ACTIVITY_CLEAR_TOP 启动 Activity 时。后台任务会怎样?

最佳答案

后台任务将继续处理。

来自文档:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

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);
}
}
};
}

我们只需要记住两件事。

  • WorkerRunnable 实际上是 Callable
  • 结果 result = doInBackground(mParams);//在后台线程处理

这就是您的答案。 doInBackground 将被处理,但 onPostExecute 可能会产生 NPE,因为父 Activity 已被销毁。

关于android - 使用 FLAG_ACTIVITY_CLEAR_TOP 启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com