onPause 在离开当前活动之前被调用

发布于 2024-12-14 13:41:42 字数 2353 浏览 1 评论 0原文

我有以下内容:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
               ....
               populate();
                handler = new Handler();
        t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        doReload1();
                        populate();
                    }
                });

            }

        }, 300, 30000);
}

private void populate() {
    if (playlists.length != 0) {
        MyListView = (ListView) findViewById(R.id.MyListView);
        for (String item : playlists) {
            Log.d("DEBUG", "item=" + item);
        }

        String[] adapterPlaylists = new String[playlists.length];
        for (int i = 0; i < playlists.length; i++) {
            adapterPlaylists[i] = playlists[i];
        }
        adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, adapterPlaylists);

        MyListView.setAdapter(adapter1);
        adapter1.notifyDataSetChanged();

        MyListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v,
                    int position, long id) {
                dialogwait = ProgressDialog.show(Playlist.this,
                        "Loading...", "Please wait..", true);
                Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
                i.putExtra("id", idPlaylist[position]);
                i.putExtra("timer", timerPlaylist[position]);
                startActivity(i);

                finish();
            }
        });
    } else
        System.out.println("playlist null");

}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("onPause Playlist!!!!!!");
    dialogwait.dismiss();
    t.cancel();
}

事情是这样的:

dialogwait = ProgressDialog.show(Playlist.this,
                            "Loading...", "Please wait..", true);

我创建了一个 ProgressDialog 并在 onPause() 中将其关闭。 但是,在创建 ProgressDialog 之前,onPause 就在 onCreate() 之后被调用。 知道为什么吗?有什么解决办法吗?谢谢

I have the following:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
               ....
               populate();
                handler = new Handler();
        t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        doReload1();
                        populate();
                    }
                });

            }

        }, 300, 30000);
}

private void populate() {
    if (playlists.length != 0) {
        MyListView = (ListView) findViewById(R.id.MyListView);
        for (String item : playlists) {
            Log.d("DEBUG", "item=" + item);
        }

        String[] adapterPlaylists = new String[playlists.length];
        for (int i = 0; i < playlists.length; i++) {
            adapterPlaylists[i] = playlists[i];
        }
        adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, adapterPlaylists);

        MyListView.setAdapter(adapter1);
        adapter1.notifyDataSetChanged();

        MyListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v,
                    int position, long id) {
                dialogwait = ProgressDialog.show(Playlist.this,
                        "Loading...", "Please wait..", true);
                Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
                i.putExtra("id", idPlaylist[position]);
                i.putExtra("timer", timerPlaylist[position]);
                startActivity(i);

                finish();
            }
        });
    } else
        System.out.println("playlist null");

}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("onPause Playlist!!!!!!");
    dialogwait.dismiss();
    t.cancel();
}

The thing is that here:

dialogwait = ProgressDialog.show(Playlist.this,
                            "Loading...", "Please wait..", true);

I create a ProgressDialog and I dissmis it in onPause().
But onPause gets called right after onCreate() before I even the ProgressDialog is created.
Any idea why?ANy solution?Thanks

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

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

发布评论

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

评论(3

无人问我粥可暖 2024-12-21 13:41:43

当前活动(对话框或任何其他活动)之上的任何内容都会使其成为后台活动,因此调用 onPause。如果有的话,应该在活动的 onResume 中调用dialog.dismiss()。在您的实现中,单击按钮时将显示一个对话框,从而暂停您的活动并调用解雇。仅当与对话框关联的进程完成时,才应调用解雇,从而告诉用户您已准备好执行其他操作 - 在您的情况下启动另一个活动。
在对话框的 onDismiss 中启动您的活动。
例如:

AlertDialog alert = new AlertDialog.Builder(this).create();
  alert.setOnDismissListener(new OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
      // start the other activity
    }
});

anything on top of your current activity (a dialog or any other activity) makes it a background activity, thus onPause is called. if anything, dialog.dismiss() should be called in activities' onResume. in your implementation, you're showing a dialog when a button is clicked, thus pausing your activity and calling dismiss. you should call dismiss only when the process associated with your dialog is finished, thus telling your user that you're ready to do something else - in your case launch another activity.
launch your activity inside your dialog's onDismiss.
for example:

AlertDialog alert = new AlertDialog.Builder(this).create();
  alert.setOnDismissListener(new OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
      // start the other activity
    }
});
寄居人 2024-12-21 13:41:42

这是因为 Android 中的 Dialog 不会阻塞 - 这意味着在其后面运行的线程(在本例中是您的 Activity,特别是您的 onItemClickListener >) 将继续执行。

看起来您想显示一个加载对话框,让用户知道他单击的项目正在加载。我建议您将该对话框移至下一个活动(从 onClick 开始的活动),然后从那里显示并关闭该对话框。

This is because a Dialog in Android, does not block - meaning that the thread running behind it (in this case your Activity and in particular your onItemClickListener) will continue to execute.

It looks like you want to display a loading dialog, to let the user know that the item he clicked is being loaded. I suggest you move that dialog to the next activity (the one started from onClick), and then display and dismiss the dialog from there.

像极了他 2024-12-21 13:41:42

我认为问题可能是您在 OnItemClickListener 中调用 finish() 。这将导致当前活动在您单击某个项目时立即结束,因为您的 onPause() 实现将立即关闭对话框。

也许您需要在创建 ViewPlaylist Activity 时而不是在此 Activity 中显示对话框。

I think that the problem could be that you are calling finish() in your OnItemClickListener. This will cause the current Activity to end as soon as you click on an item which, because of your onPause() implementation will immediately close the dialog.

Maybe you need to display the dialog when your ViewPlaylist Activity is created rather than in this Activity.

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