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

  • Home
  • SEARCH
  • 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 8507645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:55:45+00:00 2026-06-11T02:55:45+00:00

I hate inner class. I’ve a main activity who launches a ‘short-life’ AsyncTask. AsyncTask

  • 0

I hate inner class.

I’ve a main activity who launches a ‘short-life’ AsyncTask.

AsyncTask is in a separate file, is not an inner class of main activity

I need async task updates a textView from main Activity.

I know i can update a TextView from onProgressUpdate, if AsyncTask is a inner class

But how from an external, indipendent, async task ?

UPDATE: This looks like working :

In acitivty i call the task

backgroundTask = new BackgroundTask(this);
backgroundTask.execute();

In the constructor i’ve

public BackgroundTask(Activity myContext)
{
    debug = (TextView) myContext.findViewById(R.id.debugText);
}

where debug was a private field of AsyncTask.

So onProgressUpdate I can

debug.append(text);

Thanks for all of you suggestions

  • 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-11T02:55:47+00:00Added an answer on June 11, 2026 at 2:55 am

    AsyncTask is always separate class from Activity, but I suspect you mean it is in different file than your activity class file, so you cannot benefit from being activity’s inner class. Simply pass Activity context as argument to your Async Task (i.e. to its constructor)

    class MyAsyncTask extends AsyncTask<URL, Integer, Long> {
    
        WeakReference<Activity> mWeakActivity;
    
        public MyAsyncTask(Activity activity) {
           mWeakActivity = new WeakReference<Activity>(activity);
        }
    
     ...
    

    and use when you need it (remember to NOT use in during doInBackground()) i.e. so when you would normally call

    int id = findViewById(...)
    

    in AsyncTask you call i.e.

    Activity activity = mWeakActivity.get();
    if (activity != null) {
       int id = activity.findViewById(...);
    }
    

    Note that our Activity can be gone while doInBackground() is in progress (so the reference returned can become null), but by using WeakReference we do not prevent GC from collecting it (and leaking memory) and as Activity is gone, it’s usually pointless to even try to update it state (still, depending on your logic you may want to do something like changing internal state or update DB, but touching UI must be skipped).

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

Sidebar

Related Questions

I hate seeing nearly every directory in my repository list each file twice, once
I hate that google can not search for symbols. I saw this in some
I hate to sound stupid, but ASP.NET is not my forte. I have build
I have a class CFoo with a private inner class CBar. I want to
Why would anyone want to not use a code behind file so that server
I hate putting things into the functions.php file and therefore wanted to hook into
I hate case sensitivity in databases, but I'm developing for a client who uses
I hate to sound like terrible newb, but are Widgets separate apps? For instance,
I hate blocks. They are meant to make the code more concise, but I
I hate to post this but I am on a time crunch and need

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.