Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to reconnect to AsyncTask after onDestroy() and relaunch onCreate()?

I have tested the statement that AsyncTasks are not destroyed along with their launching activity. And it is true.

I made the AsyncTask just publish a Log.i() message every 3 seconds for 1 minute. And I put Log.i() messsage in the onDestroy() method of the activity.

I see the activity is destroyed, but the AsyncTask keeps running until it finishes all 20 Log.i() messages.

And I am confused.

  1. What if the AsyncTask had publishProgress() into the destroyed UI?
    I guess some sort of exception would occurr, right?

  2. What if the AsyncTask stores data in a global variable of class Application?
    No idea here, NullPointer exception?

  3. What if the app is restarted?
    It will probably launch a new AsyncTask. Can it reconnect with the still running AsyncTask?

  4. Is the AsyncTask immortal after the mother app is destroyed?
    Maybe yes, how do all LogCat apps keep logging messages while the UI application is not visible anymore, maybe destroyed? And when you reopen them they show you the messsages that were generated while it was 'dead'.

All this seems like a discussion, but the question is in the title. I have this orphan AsyncTask, which I would like very much to reconnect to when the app is relaunched, but I don't know how to do it.

I forgot to tell why this is very important. The app gets destroyed when an orientation change occurs. And I don't want to loose the data produced by the AsyncTask, I don't want to stop it and restart it. I just want it to keep going and reconnect after the orientation changes are done.

like image 381
ilomambo Avatar asked Nov 13 '22 05:11

ilomambo


1 Answers

I hope I've got this right as it's from some old code I no longer use (I now use an IntentService to do what this used to do).

This is what I originally had when I downloaded files in my main Activity...

public class MyMainActivity extends Activity {

    FileDownloader fdl = null;

    ...

    // This is an inner class of my main Activity
    private class FileDownloader extends AsyncTask<String, String, Boolean> {
        private MyMainActivity parentActivity = null;

        protected void setParentActivity(MyMainActivity parentActivity) {
            this.parentActivity = parentActivity;
        }

        public FileDownloader(MyMainActivity parentActivity) {
            this.parentActivity = parentActivity;
        }

      // Rest of normal AsyncTask methods here

    }
}

The key is to use onRetainNonConfigurationInstance() to 'save' the AsyncTask.

Override
public Object onRetainNonConfigurationInstance() {

    // If it exists then we MUST set the parent Activity to null
    // before returning it as once the orientation re-creates the
    // Activity, the original Context will be invalid

    if (fdl != null)
        fdl.setParentActivity(null);
    return(fdl);
}

I then have a method called doDownload() which is called from onResume() if a Boolean indicating downloadComplete is true. The Boolean is set in the onPostExecute(...) method of FileDownloader.

private void doDownload() {
    // Retrieve the FileDownloader instance if previousy retained
    fdl = (FileDownloader)getLastNonConfigurationInstance();

    // If it's not null, set the Context to use for progress updates and
    // to manipulate any UI elements in onPostExecute(...)
    if (fdl != null)
        fdl.setParentActivity(this);
    else {
        // If we got here fdl is null so an instance hasn't been retained
        String[] downloadFileList = this.getResources().getStringArray(R.array.full_download_file_list);
        fdl = new FileDownloader(this);
        fdl.execute(downloadFileList);
    }
}
like image 66
Squonk Avatar answered Nov 16 '22 03:11

Squonk