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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:36:42+00:00 2026-05-23T09:36:42+00:00

I have an Activity that begins executing an AsyncTask in response to clicking a

  • 0

I have an Activity that begins executing an AsyncTask in response to clicking a Button widget. I disable the Button when it’s clicked using setEnabled(false). The AsyncTask calls a callback method on the Activity when it completes. I call setEnabled(true) on the Button in the callback to re-enable the Button when the AsyncTask is done. This all works fine until the orientation changes while the AsyncTask is executing. I’ve tried lots of different things to get the Button to be enabled/disabled properly when this happens and I can’t make it work right. What is the correct way to make this work even if the orientation changes while the AsyncTask is executing?

  • 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-23T09:36:42+00:00Added an answer on May 23, 2026 at 9:36 am

    I don’t believe you need to start intercepting orientation changes. Its cutting corners and its possibly going to cause you a lot of headaches if you actually want to do things like load different resources for different orientations.

    The root problem is that the reference the AsyncTask has to the button that its meant to update is stale, it refers to your old button. What you can do to solve this is implement Activity.onRetainNonConfigurationInstanceState(). This method is called in situations when an orientation change is happening and its to allow you to store items that are non-configuration specific, like running Threads.

    The other thing you need is a method in your AsyncTask to set the Button it should enable/disable. While your Activity is restarting you unset the Button and then reset it in onCreate().

    The implementation might looks something like this.

    public class MyActivity extends Activity {
      private static final String NON_CONFIG_KEY = "com.example.NON_CONFIG_KEY";
      protected void onCreate(Bundle instanceState) {
        setContentView(R.layout.main);
        HashMap<String, Object> nonConfigState = (HashMap<String, Object>)
            getLastNonConfigurationIntstanceState();
        if (nonConfigState != null && nonConfigState.get(NON_CONFIG_KEY) != null) {
          MyAsyncTask task = (MyAsyncTask) nonConfigState.get(NON_CONFIG_KEY);
          task.setUiControl(findViewById(R.id.my_button));
        }
      }
    
      public Object onRetainNonConfigurationInstanceState() {
        task.setUiControl(null);
        // create HashMap, store AsyncTask in it, and return it
      }
    }
    
    public MyAsyncTask extends AsyncTask {
    
      private Button mControl = null;
      private boolean shouldUpdateControl = false;
      private Object mLock = new Object();
    
      public void setUiControl(Button b) {
        synchronized (mLock) {
          if (b != null && shouldUpdateControl) {
            b.setEnabled(true);
            shouldUpdateControl = false;
          }
          mControl = b;
        }
      }
    
      protected void onPostExecute(Result r) {
        synchronized (mLock) {
          if (mControl == null) {
            shouldUpdateControl = true;
          } else {
            mControl.setEnabled(true);
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that appears as a dialog using the following custom theme:
I have an activity that is a service which plays audio using a media
I have an activity that when users press share on a file it would
I have an Activity that should only get created once. That is, onCreate can
I have an activity that shows some TextView s and EditText s, as well
I have an activity that some times OutOfMemory exception occurs in some parts of
I have an activity that I'd like to load from a preference screen. I'm
I have an activity that opens txt files. And everytime a user wants to
I have an activity that contains several user editable items (an EditText field, RatingBar,
I have an activity that launches another activity with startActivityForResult method. I would like

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.