-1

i have a mapview that shows some pois with a ItemizedOverlay. When i tap on a POI, some work starts, and it takes some seconds. In the onTap method i call the handler to show the dialog, and to hide it.

I debug the code and i check that the case Util.SHOW_DIALOG: of the switch of the handler is being called correctly, but the dialog isn't being shown to the user.....

What's wrong in my code?

This is part of my MyMapActivity class, as you can see my mapview haves a handler:

public Handler dialogHandler;
private ProgressDialog progressDialog;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);             
.
.
.
/* handler para mostrar y ocultar el reloj de carga */ 
        dialogHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);               
                switch( msg.what ){
                    case Util.SHOW_DIALOG:
                        progressDialog = ProgressDialog.show(MyMapActivity.this, "" , "Cargando...", false );
                        break;
                    case Util.HIDE_DIALOG:
                        if( progressDialog != null )
                            progressDialog.dismiss();
                        progressDialog = null;
                        break;              
                }
            }
        };

And this is part of the ItemizedOverlay class, the onTap method of the ItemizedOverlay calls the handler with this code:

protected boolean onTap(int index) {
            try{
                OverlayItem item = (OverlayItem)this.mOverlays.get(index);      
                    map.dialogHandler.sendEmptyMessage(Util.SHOW_DIALOG);
.
.
//doing some work
.
.
map.dialogHandler.sendEmptyMessage(Util.HIDE_DIALOG);
Pableras84
  • 1,195
  • 5
  • 18
  • 30

1 Answers1

1

ummm...

does the "//doing some work" involve working on the same thread as the one used on the onTap ? if so, that's the ui thread , so you've sent the ui thread 2 messages - one to show and one to hide , before it could even handle them , and when it finally reaches the point it can handle them, it handles both of them , so for a very short time (probably un-noticed) the progressDialog will be shown.

anyway, use another thread for the background work , or use asyncTask .

if you use asyncTask , you can show the dialog before the work starts , and hide it when it's finished/canceled .

android developer
  • 114,585
  • 152
  • 739
  • 1,270