我的问题类似于这个 2 岁的问题,我只是重新发布相同的问题以获得更新的答案,因为两年内发生了很多变化。
我正在为 GingerBread+ 设备开发应用程序,我有很多活动,并且在后台我从服务器接收一些数据。现在基于该数据,在某些情况下我需要向Dialog
用户显示。问题是我如何知道当前最前沿的活动?
我尝试过的,
我尝试过创建getApplicationContext()
while Dialog
,但是那是行不通的。抛出一些异常。
一个办法 ?(我真的很讨厌它),Application
一个解决方案可能是通过在类中有一个变量并将其设置onResume()
为每个活动
来跟踪当前可见的活动。如果他们是实现这一目标的更聪明的方法,我真的不想做这本书,我相信他们是实现这一目标的更聪明的方法,
我的简单问题是,
如何显示有关当前可见活动的对话框?, 这样我就可以引用 AlertDialog.Builder,我认为它会完成我的工作。如果不是,我如何在最顶层的 Activity 上显示一个对话框?
编辑,我使用以下代码创建了一个简单的对话框 private View.OnClickListener cancelClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
LoginActivity.this);
// set title
alertDialogBuilder.setTitle("Roobroo will exit..");
// set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to exit ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
LoginActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
// TODO Write the code to exit from the app, (gracefull exit)
Log.i(LOG_CAT, "Cancel Button is clicked");
}
};
使用 AlertDialog.Builder 的异常 alertDialogBuilder = new AlertDialog.Builder(getApplicationContext()); 给了我以下例外,
06-11 14:09:16.732: E/AndroidRuntime(1005): FATAL EXCEPTION: main
06-11 14:09:16.732: E/AndroidRuntime(1005): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.view.ViewRoot.setView(ViewRoot.java:531)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.app.Dialog.show(Dialog.java:241)
06-11 14:09:16.732: E/AndroidRuntime(1005): at com.mycompany.myapp.activities.LoginActivity$3.onClick(LoginActivity.java:127)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.view.View.performClick(View.java:2485)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.view.View$PerformClick.run(View.java:9080)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.os.Handler.handleCallback(Handler.java:587)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.os.Looper.loop(Looper.java:123)
06-11 14:09:16.732: E/AndroidRuntime(1005): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-11 14:09:16.732: E/AndroidRuntime(1005): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 14:09:16.732: E/AndroidRuntime(1005): at java.lang.reflect.Method.invoke(Method.java:507)
06-11 14:09:16.732: E/AndroidRuntime(1005): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 14:09:16.732: E/AndroidRuntime(1005): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 14:09:16.732: E/AndroidRuntime(1005): at dalvik.system.NativeStart.main(Native Method)