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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:23:11+00:00 2026-06-04T02:23:11+00:00

I do use lots of AsyncTasks within Activities as private inner classes. One thing

  • 0

I do use lots of AsyncTasks within Activities as private inner classes. One thing that puzzles me since the beginning is what context do I get within onPostExecute()?

If you look at the stripped down example shown below you can use getString() within onPostExecute(). What context is used for that call? The Activity may have changed during doInBackground (e.g. OrientationChange) so it can’t be that context.

Currently I patch the Activity context (task.context = this) and use that specific context (context.getString()).

My question: Is it save to use getString() within onPostExecute() of an Inner Class AsyncTask without using the context of the surrounding Activity?

Many thanks in advance.

public class MyActivity extends Activity {

    /* package */ MyActivity context;

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

        public MyAsyncTask(final MyActivity context) {
            super();

            this.context = context;
        }

        @Override
        protected Cursor doInBackground(/* ... */) {
            // ...
        }

        @Override
        protected void onPostExecute(/* ... */) {
            if (context != null) {
                // Currently I do use that
                String s = context.getString(R.string.mytext);
                context.task = null;
            }

            // Would this be save?
            String s2 = getString(R.string.mytext);
        }

        @Override
        protected void onPreExecute (/* ... */) {
            // ...
        }
    }

    /* package */ MyAsyncTask  task;

    @Override
    public void onCreate(final Bundle bundle) {
        // ...

        Bundle bundleExtras = getIntent().getExtras();
        if (bundleExtras != null) {
            task = (MyAsyncTask) getLastNonConfigurationInstance();
            if (task != null) {
                task.context = this;
                // ...
            } else {
                task = new MyAsyncTask(this);
                task.execute();
            }
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        if (task != null) {
            // ...
            task.context = null;
        }

        return task;
    }
}
  • 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-04T02:23:13+00:00Added an answer on June 4, 2026 at 2:23 am

    Is it save to use getString() within onPostExecute() of an Inner Class AsyncTask without using the context of the surrounding Activity?

    No it’s not safe. Being an inner class in that activity your MyAsyncTask class you’ll have a reference to MyActivity when it is created. So, if MyActivity is killed for any reason (like rotation), in the onPostExecute callback you’ll end up with MyAsyncTask holding a reference of the killed MyActivity instance.

    Using a reference to the Activity it’s safer because you update that reference to always point to a valid instance of MyActivity. Maybe you’ll also want to invalidate the Activity directly in onRetainNonConfigurationInstance with null to make sure you don’t touch it(if the MyAsyncTask reaches the onPostExecute until you setup the MyActivity again) until you have a valid reference.

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

Sidebar

Related Questions

Lots of times when I use an application that needs perforce access (Visual Studio,
Quick question: I use lots of NSObject derived classes and am wondering how to
I need to include files within a function, and files use lots of variables
I have to create one application which has use of lots of videos &
Lots of ready-to-use character classes are available in Perl regular expressions, such as \d
I'm building a database that makes use of lots of many-to-many relations. For example,
Im building some project now that I know that I will use lots of
In my current project, we use lots of user controls. Large user controls (500+
In terms of performance and efficiency, is it better to use lots of small
People use gdb on and off for debugging, of course there are lots of

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.