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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:22:44+00:00 2026-05-31T16:22:44+00:00

I have an Activity class which has an attribute that references an AsyncTask instance

  • 0

I have an Activity class which has an attribute that references an AsyncTask instance and this AsyncTask also has a reference to this Activity.

From the AsyncTask’s onPostExecute() I want to set the reference from the Activity to the AsyncTask to null (through the reference that has the AsyncTask instance to the Activity). Is it a good practice?

I am doing it at the last line of code of the method, because I am actually saying to the garbage collector that it can delete that instance as it is no longer referenced by any other object (the Activity). Is it correct, or am I wrong?

Edit: My answer below resumes the final solution I used.

Thanks!

  • 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-31T16:22:46+00:00Added an answer on May 31, 2026 at 4:22 pm

    Why do you need to keep the reference to the AsyncTask in the first place?

    When is the reference no longer needed? What happens before the task has finished that requires you to hold a reference to the activity?

    What I am saying is, you can start the async task without a reference to it so your problem goes away. But if for some reason you do need the reference, then by explaining your particular case more, we can suggest a better solution.

    EDIT

    Your main concern is not leaking the activity, not the other way around. If you really want to clean up all references properly, I would provide an attach/detach method on the async task so that activities can attach and detach themselves from the async task when nessecary and also provide a method on the activity that nullifies the async task refence and call that from your async task if an activity is still attached. Do not do it directly. The async task should not modify the activity directly. If possible even move the async task in a new file, don’t make it an internal class of the activity, that screams of leaks.

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

Sidebar

Related Questions

I have a main Activity class that has a button to which I assign
I have class A which extends the Activity class. This class is in package
I have the following asynctask class which is not inside the activity. In the
I have 4 buttons that when click starts a new activity class. This works
I have an application that contains a main Activity, which has a very simple
I have an app which has a main activity that calls other activities on
I have a Class A which runs activity via startActivityForResult by passing Intent to
I have a non-Activity class (let's call it NonActivity) that needs to post a
I have an activity view that I have added in AppDelegate class to tap
I have a class that is its own activity that basically i use to

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.