Android - 进度对话框未关闭

发布于 2024-10-17 18:44:57 字数 724 浏览 7 评论 0原文

我有一个 ListActivity 类,当单击列表中的任何项目时,都会显示一个新活动。新活动需要时间来加载,所以我希望用户知道正在发生一些事情(以进度对话框的形式)

因此,为了做到这一点,我在我的类中实现了 Runnable,如下所示 -

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.
}

最初,当我单击,正在加载新活动,进度对话框运行良好,但是当我关闭上一个活动(返回到此列表)时,进度对话框仍在运行。我希望进度对话框仅在新活动开始时显示。

有人可以指导我如何正确执行此操作吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2024-10-24 18:44:57

对话框需要由程序员显式删除(或由用户关闭)。因此,应该这样完成:

在 Activity A(调用 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();
}

虽然在大多数用例中,繁重的工作应该在被调用者 Activity 上。万一,繁重的工作是由被调用者的 onCreate 完成的,它应该是这样的:

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();
}

无论如何,想法仍然是一样的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文