onPause 在离开当前活动之前被调用
我有以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当前活动(对话框或任何其他活动)之上的任何内容都会使其成为后台活动,因此调用 onPause。如果有的话,应该在活动的 onResume 中调用dialog.dismiss()。在您的实现中,单击按钮时将显示一个对话框,从而暂停您的活动并调用解雇。仅当与对话框关联的进程完成时,才应调用解雇,从而告诉用户您已准备好执行其他操作 - 在您的情况下启动另一个活动。
在对话框的 onDismiss 中启动您的活动。
例如:
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:
这是因为 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 yourActivity
and in particular youronItemClickListener
) 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.
我认为问题可能是您在
OnItemClickListener
中调用finish()
。这将导致当前活动在您单击某个项目时立即结束,因为您的onPause()
实现将立即关闭对话框。也许您需要在创建
ViewPlaylist
Activity 时而不是在此 Activity 中显示对话框。I think that the problem could be that you are calling
finish()
in yourOnItemClickListener
. This will cause the current Activity to end as soon as you click on an item which, because of youronPause()
implementation will immediately close the dialog.Maybe you need to display the dialog when your
ViewPlaylist
Activity is created rather than in this Activity.