Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:47:34+00:00 2026-06-04T04:47:34+00:00

I have tested the statement that AsyncTasks are not destroyed along with their launching

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:47:35+00:00Added an answer on June 4, 2026 at 4:47 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tested the below script on a demo page which is not using
i have tested standardanalyzer with indexWriter and found that it automatically removes stopwords, however,
i have made my custom Static Library (.a) successfully. i have tested it in
My MySQL database can store the euro symbol just fine (as I have tested
I made a few tests, first I tested mysql prepared statement with $pdo->prepare() and
I'm building a website and I have tested this sql statement on the production
I have a simple CRUD operation that needs to be unit tested. These test
I have tested all my systems developed in Delphi (in Windows XP) on Wine
I have tested this servlet and it works well, except in Google Chrome it
I have tested my application on various mobile phones. My applications main functionality is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.