Android AsyncTasks How to Check If activity is still running

android, android-asynctask

Solution

You can cancel your asynctask in the activity's onDestroy

@Override
protected void onDestroy() {
    asynctask.cancel(true);
    super.onDestroy();
}

and when performing changes you check whether your asynctask has been cancelled(activity destroyed) or not

protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);
    if(!isCancelled()) {
         gender.setText(values[0]);
    }
}

Problem

I have used `AsyncTasks` with my application, in order to lazy download and update the UI. For now my `AsyncTasks` updates the UI real simply: ``` protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); gender.setText(values[0]); } ``` My problem is how to check if the activity which the gender `TextView` rendered from, is still available? If not, I will get an error and my application will shut down.

Original source

Related problems