Search code examples
progressdialogandroidrunnable

Android - progress dialog does not close


I have a ListActivity class, and when any item on the list is clicked, a new activity is displayed. The new activity takes time to load, so i would like the users to know that there is something happening (in the form of a progress dialog)

So, in order to do this, i implemented Runnable in my class like this -

public class ProtocolListActivity extends ListActivity implements Runnable {
private ProgressDialog progDialog;
....
protected void onListItemClick(ListView l, View v, int position, long id) {
                    progDialog.show(this, "Showing Data..", "please wait", true, false);

    Thread thread = new Thread(this);
    thread.start();
}
....
public void run() {
     // some code to start new activity based on which item the user has clicked.
}

Initially, when i click, and the new activity is being loaded, the progress dialog works nicely, but when i close the previous activity (to get back to this list) the progress dialog is still running. I want the progress dialog to show up only for the time the new activity is being started.

Can someone please guide me on how to do this correctly.


Solution

  • Dialogs needs to be explicitly removed by programmer (or close by user). So, it should be done in this way:

    in Activity A (calling activity)

    protected void onListItemClick(ListView l, View v, int position, long id) {
        progDialog.show(this, "Showing Data..", "please wait", true, false);
    
        Thread thread = new Thread(this){
            // Do heavy weight work
    
            // Activity prepared to fire
    
            progDialog.dismiss();
        };
        thread.start();
    }
    

    Although in most use case, the heavy work should be on the callee Activity. In case, the heavy work is done onCreate of the callee, it should be like:

    Activity B (Callee):

    onCreate(){
        progDialog.show(this, "Showing Data..", "please wait", true, false);
    
        Thread thread = new Thread(this){
            // Do heavy weight work
    
            // UI ready
    
            progDialog.dismiss();
        };
        thread.start();
    }
    

    Anyway, the idea is still the same.