1

I am quite new to Android programming, so please excuse me for the stupid question >,< Just like the title says, I want to show a AlertDialog/Toast right after my ProgressDialog finishes. How I can do this in the proper way? Note: My handler is only to dismiss the ProgressDialog (pd.dismissDialog()).

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnDoRegister:
    pd = ProgressDialog.show(RegisterActivity.this, "",
                    "Registering with the server..");
            new Thread() {
                public void run() {
                    Looper.prepare();
                    //(hopefully) thread-safe implementations here
                    handler.sendEmptyMessage(0);
                }
            }.start();
            if (status == registered) {
                //show AlertDialog/Toast here
                finish();
            } else if (status == notRegistered) {
                //show AlertDialog/Toast here
            } 
     break; 
   }

What is more confusing is, when I tried to debug this, LogCat shows nothing and everything was running perfectly (the alert/toast shows up as expected). But when I tried to run it normally, somehow I feel that this runs too fast and not showing my alert/toast correctly (sounds stupid). Thank you very much for your help.

nic
  • 2,125
  • 1
  • 18
  • 33

2 Answers2

1

It will be more suitable to show the AlertDialog/Toast in the handleMessage method of the handler,

static Handler handler = new Handler() {
       public void handleMessage(Message msg) { ...  }};
mihail
  • 2,173
  • 19
  • 31
  • You are right. After I call the AlertDialog/Toast inside the Handler of the thread, it works normally! Thank you very much for the help :) – nic Sep 09 '11 at 11:37
0

It happens because, your thread is finishing its work soon when you run it. but when you debug its like other procedural languages where you execute things sequentially one after the other. but if you still want it show dialog for long time, do this (Though its of no Practical use)

new Thread() {
                public void run() {
                    Looper.prepare();
                    //(hopefully) thread-safe implementations here
                    handler.sendEmptyMessage(0);
                     Thread.sleep(3000);
                }
            }.start();
ngesh
  • 13,398
  • 4
  • 44
  • 60