How To Kill A Activity And The Asynctask
Solution 1:
You need to cancel the AsyncTask
in the Activity's onStop
.
Solution 2:
You need to implement two things to stop the AsyncTask execution:
1. call Cancel() method of AsyncTask from where you want to stop the execution. This could be in onStop()
of your Activity , like below:
asyncTask.cancel(true);
2. Now you have to check whether the AsyncTask is cancelled or not by using isCancelled method inside the doInBackground method.
protectedObjectdoInBackground(Object... x) {
while (/* condition */)
{
...
if (isCancelled())
break;
}
returnnull;
}
Why? Because of below description from Android docs for AsyncTask:
Cancelling a task 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.)
Solution 3:
you can refer to this link for cancelling your async task
Solution 4:
I would recommend using surface to code the actual game part instead of activity, that might help you get rip of this issue.
Solution 5:
In the code to handle click
- 1> Call finish(), and use AsyncTask.cancel() method on onStop() or onDestroy().
- or 2> use AsyncTask.cancel(), and in AsyncTask.onCancelled() method, call finish() to end activity.
1> is common used.
Post a Comment for "How To Kill A Activity And The Asynctask"