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");
}
}
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
AsyncTaskto 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
onDestroymethod 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: