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 6254273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:09:12+00:00 2026-05-24T14:09:12+00:00

I have an activity which, during its execution, kicks off a couple of AsyncTask

  • 0

I have an activity which, during its execution, kicks off a couple of AsyncTasks and a couple of Threads.

If these continue when the activity is destroyed/recreated then all sorts of errors occur so my bright idea was to make each non UI thread register itself, adding itself to an ArrayList<Thread>() and ArrayList<AsyncTask>() meaning that I could, onDestroy(), iterate through these ArrayLists and destroy them like so:

@Override
public void onDestroy() 
{
    for(AsyncTask task : tasks)
        task.cancel(true);*

    for(Thread thread : threads)
        thread.interrupt();
}

Unfortunately this throws a java.util.ConcurrentModificationException on the line marked with *. I tried wrapping each for loop in a synchronize block to make sure the ArrayLists aren’t being modified by any other Threads.

Do AsyncTasks destroy themselves on completion (and therefore throw the above error)?

Does anyone know of a better way of controlling Threads in an Android environment?

EDIT

Changing the code to use for(int i =0 : i < array.size() ; i++) seems to solve this problem but I would still like to know /why/ it happens:

    synchronized(tasks)
    {
        for(int i=0; i < tasks.size(); i++)
            tasks.get(i).cancel(true);
    }

    synchronized(threads)
    {
        for(int i=0; i < threads.size(); i++)
            threads.get(i).interrupt();
    }
  • 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-05-24T14:09:13+00:00Added an answer on May 24, 2026 at 2:09 pm

    As far as I can tell, AsyncTasks disappear, once they’ve run the onPostExecute and you’ve set the instance to null, or something similar. What are you doing in a Thread that you couldn’t do in a Service or AsyncTask? Are you doing UI operations in your threads (like, runOnUIThread or something) that would cause these threads to error when the app isn’t in foreground?

    The nice thing about doing things in a Service, is you can broadcast that you’re about to close the app, and stop any actions in the Service that you no longer need running, essentially hibernating the Service until you fire it up again when the app comes into foreground.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a login page (an activity in Android), during which I run an
I have an android application which in its main activity, data are adapted from
I have an activity which is going to switch around between several views, and
I have a activity which has a button and 2 autocomplete widget. for the
I have an Activity which shows a welcome message if started for the first
I have an activity which shows some List entries. When I click on a
I have an activity which needs to make two remote server calls. The first
I have an activity which uses two Loaders. Each of them returns different type
I have an activity which consists of a ListView . The problem is that
I have an Activity which uses a Fragment . I simply want to pass

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.