[英]onPause gets called before leaving the current activity
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()
. 我创建一个
ProgressDialog
并在onPause()
分发它。 But onPause
gets called right after onCreate()
before I even the ProgressDialog
is created. 但是
onPause
会在onCreate()
之后被调用,甚至在我创建ProgressDialog
之前。 Any idea why?ANy solution?Thanks 知道为什么吗?有解决方案吗?
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. 这是因为Android中的
Dialog
不会阻塞-意味着在其后面运行的线程(在这种情况下,您的Activity
尤其是onItemClickListener
)将继续执行。
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.
我建议您将该对话框移至下一个活动(该活动从onClick开始),然后从那里显示并关闭该对话框。
I think that the problem could be that you are calling finish()
in your OnItemClickListener
. 我认为问题可能是您在
OnItemClickListener
中调用finish()
。 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. 这将导致当前的活动在您单击某个项目后立即结束,由于您的
onPause()
实现,该项目将立即关闭对话框。
Maybe you need to display the dialog when your ViewPlaylist
Activity is created rather than in this Activity. 也许您需要在创建
ViewPlaylist
活动ViewPlaylist
不是在此活动中显示对话框。
anything on top of your current activity (a dialog or any other activity) makes it a background activity, thus onPause is called. 当前活动之上的任何内容(对话框或任何其他活动)都会使其成为后台活动,因此调用了onPause。 if anything, dialog.dismiss() should be called in activities' onResume.
如果有的话,应该在活动的onResume中调用dialog.dismiss()。 in your implementation, you're showing a dialog when a button is clicked, thus pausing your activity and calling dismiss.
在您的实现中,单击按钮时会显示一个对话框,从而暂停您的活动并调用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.
您应该仅在与对话框关联的过程完成后才调用dismiss,从而告诉您您准备做其他事情-在您的情况下,启动另一个活动。 launch your activity inside your dialog's onDismiss.
在对话框的onDismiss中启动您的活动。 for example:
例如:
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// start the other activity
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.