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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:02:51+00:00 2026-06-02T21:02:51+00:00

I have spent many hours looking for a solution to this and need help.

  • 0

I have spent many hours looking for a solution to this and need help.

I have a nested AsyncTask in my Android app Activity and I would like to allow the user to rotate his phone during it’s processing without starting a new AsyncTask. I tried to use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance().

I am able to retain the task; however after rotation it does not save the result from onPostExecute() to the outer class variable. Of course, I tried getters and setters. When I dump the variable in onPostExecute, that it is OK. But when I try to access to the variable from onClick listener then it is null.

Maybe the code will make the problem clear for you.

public class MainActivity extends BaseActivity {

    private String possibleResults = null;
    private Object task = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            this.task = getLastNonConfigurationInstance();

            setContentView(R.layout.menu);

            if ((savedInstanceState != null)
                            && (savedInstanceState.containsKey("possibleResults"))) {
                    this.possibleResults = savedInstanceState
                                    .getString("possibleResults");
            }

            if (this.possibleResults == null) {
                    if (this.task != null) {
                            if (this.task instanceof PossibleResultWebService) {
                                    ((PossibleResultWebService) this.task).attach();
                            }

                    } else {
                            this.task = new PossibleResultWebService();
                            ((PossibleResultWebService) this.task).execute(this.matchToken);
                    }
            }

            Button button;
            button = (Button) findViewById(R.id.menu_resultButton);
            button.setOnClickListener(resultListener);
    }

    @Override
    protected void onResume() {
            super.onResume();
    }

    OnClickListener resultListener = new OnClickListener() {
            @Override
            public void onClick(View v) {

                    Spinner s = (Spinner) findViewById(R.id.menu_heatSpinner);
                    int heatNo = s.getSelectedItemPosition() + 1;
                    Intent myIntent = new Intent(MainActivity.this,
                                    ResultActivity.class);
                    myIntent.putExtra("matchToken", MainActivity.this.matchToken);
                    myIntent.putExtra("heatNo", String.valueOf(heatNo));
                    myIntent.putExtra("possibleResults",
                                    MainActivity.this.possibleResults);

                    MainActivity.this.startActivityForResult(myIntent, ADD_RESULT);
            }
    };

    private class PossibleResultWebService extends AsyncTask<String, Integer, Integer> {

            private ProgressDialog pd;
            private InputStream is;
            private boolean finished = false;
            private String possibleResults = null;

            public boolean isFinished() {
                    return finished;
            }

            public String getPossibleResults() {
                    return possibleResults;
            }

            @Override
            protected Integer doInBackground(String... params) {
                // quite long code
            }

            public void attach() {
                    if (this.finished == false) {
                            pd = ProgressDialog.show(MainActivity.this, "Please wait...",
                                            "Loading data...", true, false);
                    }
            }

            public void detach() {
                    pd.dismiss();
            }

            @Override
            protected void onPreExecute() {
                    pd = ProgressDialog.show(MainActivity.this, "Please wait...",
                                    "Loading data...", true, false);
            }

            @Override
            protected void onPostExecute(Integer result) {
                    possibleResults = convertStreamToString(is);
                    MainActivity.this.possibleResults = possibleResults;

                    pd.dismiss();
                    this.finished = true;
            }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

            super.onSaveInstanceState(outState);

            if (this.possibleResults != null) {
                    outState.putString("possibleResults", this.possibleResults);
            }

    }

    @Override
    public Object onRetainNonConfigurationInstance() {
            if (this.task instanceof PossibleResultWebService) {
                    ((PossibleResultWebService) this.task).detach();
            }
            return (this.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-02T21:02:52+00:00Added an answer on June 2, 2026 at 9:02 pm

    It is because you are creating the OnClickListener each time you instantiate the Activity (so each time you are getting a fresh, new, OuterClass.this reference), however you are saving the AsyncTask between Activity instantiations and keeping a reference to the first instantiated Activity in it by referencing OuterClass.this.

    For an example of how to do this right, please see https://github.com/commonsguy/cw-android/tree/master/Rotation/RotationAsync/

    You will see he has an attach() and detach() method in his RotationAwareTask to solve this problem.

    To confirm that the OuterClass.this reference inside the AsyncTask will always point to the first instantiated Activity if you keep it between screen orientation changes (using onRetainNonConfigurationInstance) then you can use a static counter that gets incremented each time by the default constructor and keep an instance level variable that gets set to the count on each creation, then print that.

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

Sidebar

Related Questions

I have spent many hours looking into this and I still can't find a
I have spent many hours trying to work this damn thing out! so I
I have spent too many hours on this and I have so far continued
I have spent many hours working on this program that is supposed to play
I've spent many hours trying to get this to work. And I'm getting quite
I don't even want to think about how many man hours have been spent
Roughly I have spent 2 hours and concert many question answers regarding IE7 debugging
i have spent a few hours on this to no avail. I have an
Urgh, I have spent the last couple of hours on this now. I normally
I have spent many hours trying to determine a formula to convert .NET pixels

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.