Skip to content

Commit 615a805

Browse files
committed
updates
1 parent ce03244 commit 615a805

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

‎de.vogella.android.handler/src/de/vogella/android/handler/ProgressTestActivity.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public void onCreate(Bundle savedInstanceState) {
1919
setContentView(R.layout.main);
2020
progress = (ProgressBar) findViewById(R.id.progressBar1);
2121
text = (TextView) findViewById(R.id.textView1);
22-
handler = new Handler();
22+
// Reuse existing hander from Activity
23+
// Avoid the creation of a new object via new Handler();
24+
handler = getWindow().getDecorView().getHandler();
2325

2426
}
2527

‎de.vogella.android.threadslifecycle/src/de/vogella/android/threadslifecycle/ThreadsLifecycleActivity.java‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313

1414
import android.app.Activity;
1515
import android.app.ProgressDialog;
16+
import android.content.Context;
1617
import android.graphics.Bitmap;
1718
import android.graphics.BitmapFactory;
1819
import android.os.Bundle;
1920
import android.os.Handler;
21+
import android.os.Message;
2022
import android.view.View;
2123
import android.widget.ImageView;
2224

2325
public class ThreadsLifecycleActivity extends Activity {
2426
// Static so that the thread access the latest attribute
2527
private static ProgressDialog dialog;
26-
private static ImageView imageView;
2728
private static Bitmap downloadBitmap;
2829
private static Handler handler;
30+
private ImageView imageView;
2931
private Thread downloadThread;
3032

3133
/** Called when the activity is first created. */
@@ -35,9 +37,18 @@ public void onCreate(Bundle savedInstanceState) {
3537
super.onCreate(savedInstanceState);
3638
setContentView(R.layout.main);
3739
// Create a handler to update the UI
38-
handler = new Handler();
40+
handler = new Handler() {
41+
@Override
42+
public void handleMessage(Message msg) {
43+
imageView.setImageBitmap(downloadBitmap);
44+
dialog.dismiss();
45+
}
46+
47+
};
3948
// get the latest imageView after restart of the application
4049
imageView = (ImageView) findViewById(R.id.imageView1);
50+
Context context = imageView.getContext();
51+
System.out.println(context);
4152
// Did we already download the image?
4253
if (downloadBitmap != null) {
4354
imageView.setImageBitmap(downloadBitmap);
@@ -110,7 +121,8 @@ public void run() {
110121
e.printStackTrace();
111122
}
112123
downloadBitmap = downloadBitmap("http://www.devoxx.com/download/attachments/4751369/DV11");
113-
handler.post(new MyRunnable());
124+
// Updates the user interface
125+
handler.sendEmptyMessage(0);
114126
} catch (IOException e) {
115127
e.printStackTrace();
116128
} finally {
@@ -119,11 +131,4 @@ public void run() {
119131
}
120132
}
121133

122-
static public class MyRunnable implements Runnable {
123-
public void run() {
124-
imageView.setImageBitmap(downloadBitmap);
125-
dialog.dismiss();
126-
}
127-
}
128-
129134
}

0 commit comments

Comments
 (0)