4

我对异步任务感到困惑。当我的活动重新开始时,我需要做什么?在我的活动中, onCreate() 启动了一个异步任务。我知道活动会在 android 需要时重新启动(即方向更改或其他)。我对此没有任何问题......而且我认为重新启动新的异步任务是可以接受的。

不过,我不知道我之前的异步任务发生了什么。我有摧毁它吗?

我的第二个问题:如果我在上一个任务中有一个 progressDialog 怎么办。我是否必须关闭此对话框(以及如何关闭)?

4

2 回答 2

2

不,您的Asynctask遗嘱以您的活动以及您的progressDialog. 当您的活动调用onRestart()时,它必须首先通过onPauseonStop这将破坏您的活动,但不会破坏您的应用程序。

有关活动的更多信息 - http://developer.android.com/reference/android/app/Activity.html

Asynctask此外,取消您的以及设置progressDialog为空会更安全。

Asynctask文档

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

于 2013-02-13T10:05:09.173 回答
0

您必须保存活动实例,当您的活动重新启动时,您必须使用该实例恢复您的活动,请使用此链接: 在 Android 中保存一些关于方向更改的数据, 您必须关闭该对话框。

于 2013-02-13T10:11:22.183 回答