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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:08:58+00:00 2026-06-11T06:08:58+00:00

I have managed to use an Asynctask with an indeterminate progress bar during screen

  • 0

I have managed to use an Asynctask with an indeterminate progress bar during screen rotation. Asynctask starts only once, progress bar is restored on rotation just as I wanted.

I have different layouts for portrait and layout orientations. Layouts include a button and a textview. The size and text color of textview in layout-land is different. And the orientation is landscape.

The problem is when I rotate the screen while asynctask is running, it cant update the textview in onPostExecute method. When I rotate, it recreates the activity with layout-land file. But why I cant update my Textview?

layout\activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <Button 
        android:text="Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startClicked"
        />
    <TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</LinearLayout>

layout-land\activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal">

    <Button 
        android:text="Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startClicked"
        />
    <TextView
        android:textSize="36dp"
        android:textColor="#ff0000"
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</LinearLayout>

MainActivity.java:

package com.example.asynctaskconfig;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

    public class MainActivity extends Activity {
        static String data;
        static ProgressDialog pd;
        MyAsyncTask task;
        TextView tv;

        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_main);

            tv = (TextView) findViewById(R.id.hello);

            if (getLastNonConfigurationInstance() != null) {
                task = (MyAsyncTask) getLastNonConfigurationInstance();
                if (task != null) {
                    if (!(task.getStatus().equals(AsyncTask.Status.FINISHED))) {
                        showProgressDialog();
                    }
                }
            }
        }

        @Override
        public Object onRetainNonConfigurationInstance() {
            if (pd != null)
                pd.dismiss();
            if (task != null)
                return (task);
            return super.onRetainNonConfigurationInstance();
        }


        private void showProgressDialog() {
            if (pd == null || !pd.isShowing()) {
                pd = new ProgressDialog(MainActivity.this);
                pd.setIndeterminate(true);
                pd.setTitle("DOING..");
                pd.show();
            }
        }

        private void dismissProgressDialog() {
            if (pd != null && pd.isShowing())
                pd.dismiss();
        }

        public class MyAsyncTask extends AsyncTask<String, Void, Boolean> {
            @Override
            protected void onPreExecute() {
                showProgressDialog();
            }

            @Override
            protected Boolean doInBackground(String... args) {
                try {
                    Thread.sleep(5000);
                    data = "result from ws";
                } catch (Exception e) {
                    return true;
                }
                return true;
            }

            protected void onPostExecute(Boolean result) {
                if (result) {
                    dismissProgressDialog();
                    updateUI();
                }
            }
        }

        private void updateUI() {
            tv.setText(data == null ? "null" : data);
        }

        public void startClicked(View target) {
            task = new MyAsyncTask();
            task.execute("start");
        }
    }
  • 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-11T06:09:00+00:00Added an answer on June 11, 2026 at 6:09 am

    What I have done is as follows:

    1- Add android:freezesText="true" to all my TextViews. This enables TextViews to save their states on configuration changes.

    2- Make your AsyncTask a static inner class.

    3- Modify AsyncTask to keep a reference to the Activity it lives in. So AsyncTask can access UI widgets of Activity via this reference.

    4- Here, it is important to keep a valid activity reference during screen rotations. So, override onDestroy method and unbind the Activity from AsyncTask. Thus, task wont keep the old(died) activity.

    5- In onRetainNonConfigurationInstance, if task is still running, update its activity reference with the current activity, so it is successfully tied to new activity.

    6- Finally, in onPostExecuteMethod, access the UI elements of the Activity via activity reference.

    Complete Working Solution:

    layout\activity_main.xml :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical">
    
        <Button 
            android:text="Start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startClicked"
            />
        <TextView
            android:freezesText="true"
            android:id="@+id/hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            tools:context=".MainActivity" />
    </LinearLayout>
    

    layout-land\activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="horizontal">
    
        <Button 
            android:text="Start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startClicked"
            />
        <TextView
            android:freezesText="true"
            android:textSize="36dp"
            android:textColor="#ff0000"
            android:id="@+id/hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            tools:context=".MainActivity" />
    </LinearLayout>
    

    MainActivity.java:

    package com.example.asynctaskconfig;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        static ProgressDialog pd;
        MyAsyncTask task;
        TextView tv;
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_main);
    
            tv = (TextView) findViewById(R.id.hello);
    
            if (getLastNonConfigurationInstance() != null) {
                task = (MyAsyncTask) getLastNonConfigurationInstance();
                if (task != null) {
                    task.activity = this;
                    if (!(task.getStatus().equals(AsyncTask.Status.FINISHED))) {
                        showProgressDialog();
                    }
                }
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (task != null) {
                task.activity = null;
            }
        }
    
        @Override
        public Object onRetainNonConfigurationInstance() {
            if (pd != null)
                pd.dismiss();
            if (task != null)
                return (task);
            return super.onRetainNonConfigurationInstance();
        }
    
        private void showProgressDialog() {
            if (pd == null || !pd.isShowing()) {
                pd = new ProgressDialog(MainActivity.this);
                pd.setIndeterminate(true);
                pd.setTitle("DOING..");
                pd.show();
            }
        }
    
        private void dismissProgressDialog() {
            if (pd != null && pd.isShowing())
                pd.dismiss();
        }
    
        static class MyAsyncTask extends AsyncTask<String, Void, String> {
            MainActivity activity;
    
            public MyAsyncTask(MainActivity activity) {
                this.activity = activity;
            }
    
            @Override
            protected void onPreExecute() {
                activity.showProgressDialog();
            }
            @Override
            protected String doInBackground(String... args) {
                try {
                    Thread.sleep(8000);
                    return "data from ws";
                } catch (Exception e) {
                    return "exception";
                }
            }
    
            protected void onPostExecute(String result) {
                activity.dismissProgressDialog();
                activity.tv.setText(result == null ? "null" : result);
            }
        }
    
        public void startClicked(View target) {
            task = new MyAsyncTask(this);
            task.execute("start");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a file manager-like application where we use the action bar home button
I have just found a nice look that I would like to use for
I am just picking up on using jquery and have managed to put the
I have this code and i would like to use asyncTask manager with this
After weeks of effort I have managed to write F# programs that use LLVM
So I have managed to find another question discussing how to use the libjpeg
I have managed to use .htaccess to redirect the domain url to a page
I've seen some people that have managed to use the themes in the Silverlight
I am trying to use list views and I have managed to create one
I have managed to use the answer here to rotate the camera preview 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.