开发者

Android - progress dialog does not close

开发者 https://www.devze.com 2023-02-12 08:42 出处:网络
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 happe

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 acti开发者_高级运维vity 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号