title: Android AsyncTask ProgressDialog rotation different configurations
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p><strong>layout\activity_main.xml:</strong></p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;Button 
          android:text="Start"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:onClick="startClicked"
          /&gt;
      &lt;TextView
          android:id="@+id/hello"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world"
          tools:context=".MainActivity" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>layout-land\activity_main.xml:</strong></p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;Button 
          android:text="Start"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:onClick="startClicked"
          /&gt;
      &lt;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" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>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 &amp;&amp; pd.isShowing())
                  pd.dismiss();
          }
  
          public class MyAsyncTask extends AsyncTask&lt;String, Void, Boolean&gt; {
              @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");
          }
      }
  </code></pre>