Wrong language? Choose different language instead:

close
Android Forum » Android Developer Forum » Android Code Snippets » AsyncTask: ProgressDialog und sauberes OrientationChange Handling (InnerClass Version)

AsyncTask: ProgressDialog und sauberes OrientationChange Handling (InnerClass Version)

User-Foto
Gelöschter Account
AsyncTask: ProgressDialog und sauberes OrientationChange Handling (InnerClass Version)
verfasst am 07.08.2012 17:48:06 — geändert am 09.10.2012 17:12:46
Immer wieder im Forum nachgefragt: AsyncTask in einer Activity nutzen und sauberes OrientationChange (ohne den Manifest Hack).

1.) In res/values/styles.xml aufnehmen:

1<resources>
2 <style name="MyProgressDialog" parent="@android:style/Theme.Dialog">
3 <item name="android:background">@android:color/transparent</item>
4 <item name="android:backgroundDimEnabled">false</item>
5 <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
6 <item name="android:windowBackground">@android:color/transparent</item>
7 <item name="android:windowContentOverlay">@null</item>
8 <item name="android:windowFrame">@null</item>
9 <item name="android:windowIsFloating">true</item>
10 <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
11 <item name="android:windowTitleStyle">@null</item>
12 </style>
13</resources>

2.) Neue Klasse MyProgressDialog.java:

1public class MyProgressDialog extends Dialog {
2
3 public MyProgressDialog(Context context) {
4 super(context, R.style.MyProgressDialog);
5 }
6
7 public static MyProgressDialog show(Context context, CharSequence title, CharSequence message) {
8 return show(context, title, message, false);
9 }
10
11 public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate) {
12 return show(context, title, message, indeterminate, false, null);
13 }
14
15 public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) {
16 return show(context, title, message, indeterminate, cancelable, null);
17 }
18
19 public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, final OnCancelListener onCancelListener) {
20 MyProgressDialog dialog = new MyProgressDialog(context);
21 dialog.setCancelable(cancelable);
22 dialog.setOnCancelListener(onCancelListener);
23 dialog.setTitle(title);
24
25 dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
26 dialog.show();
27
28 return dialog;
29 }
30}

3.) Die Activity mit InnerClass AsyncTask. Wird die Activity abgeschossen während AsyncTask läuft, so wird der ProgressDialog weggeworfen und nach dem Neustart wird er wieder aufgebaut. Der Context wird nach dem Neustart der Activity in den AsyncTask "gepatcht". AsyncTask greift ausschließlich über diesen Context auf die Activity zu und muss diesen auch immer auf != null prüfen.

1public class MyActivity extends Activity {
2
3 private class MyAsyncTask extends AsyncTask<..., ..., ...> {
4
5 private Context context;
6 private MyProgressDialog dialog;
7
8 public MyAsyncTask(Context context) {
9 super();
10
11 this.context = context;
12 }
13
14 @Override
15 protected ... doInBackground(...) {
16 // Lang laufender Job ohne UI Nutzung
17 }
18
19 @Override
20 protected ... onPostExecute(...) {
21 if (dialog != null) {
22 try {
23 dialog.dismiss();
24 } catch (Exception exception) {
25 }
26
27 dialog = null;
28 }
29
30 if (context != null) {
31 // Hier UI Nutzung
32 context.eineMethode();
33 }
34
35 // Signalisiert der Activity: AsyncTask laeuft nicht mehr
36 if (context != null) {
37 context.task = null;
38 }
39 }
40
41 @Override
42 protected ... onPreExecute () {
43 // ProgressDialog ausgeben
44 if (context != null) {
45 dialog = MyProgressDialog.show(context, null, null, true, false);
46}
47 }
48 }
49
50 private MyAsyncTask task;
51
52 @Override
53 public void onCreate(Bundle bundle) {
54 // ...
55
56 task = (MyAsyncTask) getLastNonConfigurationInstance();
57 if (task != null) {
58 // Task war aktiv als Activity abgeschossen wurde, Context in AsyncTask setzen und ProgressDialog anzeigen
59 task.context = this;
60 task.dialog = MyProgressDialog.show(this, null, null, true, false);
61 } else {
62 // Task war nicht aktiv, Task starten (z.B. bei einer ListActivity, etc.)
63 task = new MyAsyncTask(this);
64 task.execute();
65 }
66
67 // ...
68 }
69
70 @Override
71 public Object onRetainNonConfigurationInstance() {
72 if (task != null) {
73 // Activity wird abgeschossen waehrend Task laeuft
74 if (task.dialog != null) {
75 // ProgressDialog wegwerfen
76 task.dialog.dismiss();
77 task.dialog = null;
78 }
79
80 // Signalisiert dem AsyncTask: Activity existiert nicht mehr
81 task.context = null;
82 }
83
84 return task;
85 }
86
87 private void eineMethode() {
88 }
89}

-----
Meine Apps: Tankbuch Mobil Pro | GaCoMo for Garmin Connect | Streaming Repeater | Ultraviewer

Antworten mit Zitat Antworten Link +1     (1 Stimme)