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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:22:18+00:00 2026-05-20T02:22:18+00:00

Just curious how people are handling this situation: Let’s say when your Activity is

  • 0

Just curious how people are handling this situation:

Let’s say when your Activity is created you launch an AsyncTask or Thread to hit a web service and retrieve a piece of data. Suppose, before the AsyncTask finishes, the user has hit some button that sends them to a new Activity. While on the new Activity, the AsyncTask finishes.

When the AsyncTask finishes, you want a member variable of the first Activity to update, but that Activity is now stopped.

What do you do? As I understand it, you can’t safely access the member variable because Android could have reclaimed it. You could use a BroadcastReceiver, but if you’re no longer on that Activity, the BR won’t receive the message. So what is the right answer?

Update:

A possibility is to have the task write to a database and read from that database in the Activity’s onResume(). However, in my case, this is not appropriate. The data in my Activity is not to be written (read: saved) to the database until the user is finished with that Activity and has clicked a “save” button.

  • 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-20T02:22:18+00:00Added an answer on May 20, 2026 at 2:22 am

    As I understand it, you can’t safely
    access the member variable because
    Android could have reclaimed it.

    Nope, as long as you have a reference to an object (including Activity instances) then it still exists. Your first Activity still exists while it is in the stopped state… it is fine for your AsyncTask to update UI elements or any associated data.

    It is however possible for your Activity to be permanently removed from your task while your AsyncTask is running… perhaps this is what you were thinking of?

    The most common case is when the user simply changes the orientation of the phone. Your activity will likely be destroyed and recreated anew. If you have data which needs to survive this – it’s likely that you do – then implement onRetainNonConfigurationInstance() and call getLastNonConfigurationInstance() from onCreate() to handle this. (Obviously your AsyncTask that’s running through all of this should know to update things on the later instance, not the previous one).

    Finally, if your AsyncTask does update UI (as seems likely) then it’s better for it to keep WeakReferences on UI objects rather than direct references. Activities are expensive objects, and as long as something’s keeping a reference to one (and every View contains a reference to it’s owning Context/Activity), it can’t be GC’d. Very easy to leak memory that way.

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

Sidebar

Related Questions

Just curious how many people are actually running 2010 and what's your feedback so
I'm using Postgres, but I'm curious about this just in general. People talk about
I'm just curious how most people make their ASP.NET pages printer-friendly? Do you create
Just curious how you would comment this line of code: string customerNm = customerNm.EndsWith(s)
I'm just curious as to what methods people are using to dynamically use larger
Just curious what people would name the file that contains the generic interface IRepository<T>
Just curious to see people's opinions: Has anyone thought about implementing the Flash player
So, let's say that I have an element that is inline-block. So this won't
I just realized static events exist - and I'm curious how people use them.
I wrote an iphone app and was just released.curious to See how many people

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.