Skip to content Skip to sidebar Skip to footer

Listadapter Lifecycle And Screen Rotation For Canceling A Asynctask

I have a asynctask that I cancel when the view is destoried via onDestoryView(). This problem is I do 'downloader.cancel(true);' and it wont cancel. In fact, it will return false.

Solution 1:

Cancelling an AsyncTask does not kill the Thread. You will see in the docs for Thread that methods like stop and destroy are unimplemented. So once the doInBackground method starts executing, it will run to completion even if the task is cancelled with cancel(true). You will need to code it appropriately.

Solution 2:

When the screen orientation is changed the default behavior is destroy the Activity and recreate it. So the methods OnDestroy, and OnCreate will be called. You can cancel this behavior adding this line in the activity that of your your Manifest:

android:configChanges="keyboardHidden|orientation"

Like that:

<activityandroid:name=".YourActivity"android:label="@string/app_name"android:configChanges="keyboardHidden|orientation" >

You also needs to add the method onConfigurationChanged in your activity class.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig); // tem que terreconfiguraInterface();
}

After that OnDestroy and OnCreate will not be called when orientation changes.

Post a Comment for "Listadapter Lifecycle And Screen Rotation For Canceling A Asynctask"