My problem is similar to this two-year question , I just post the same problem to get updated answers, since a lot has changed in two years.
Iβm developing an application for GingerBread + devices, I have a lot to do, and in the background I get some data from the server. Now, based on this data, in some cases I need to show the Dialoguser. The problem is how to find out which front is currently the most active?
What I tried,
I tried to give getApplicationContext()during creation Dialog, however this does not work. Throwing some kind of exception.
Decision? (I really hate this)
The solution may be to keep track of the current visible activity, having a variable in the class Applicationand setting it on onResume()each activity. I really donβt want to do this bookkeeping if these are more reasonable ways to achieve this, and Iβm sure that these are more reasonable ways to achieve this,
My simple question is:
How do I display the "Currently Visible Activity" dialog box? so that I can give this link to AlertDialog.Builder, which I think will work. If not, how can I display the dialog box in Activity?
Edit, I am creating a simple dialog using the following private View.OnClickListener code cancelClickListener = new OnClickListener () {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
LoginActivity.this);
alertDialogBuilder.setTitle("Roobroo will exit..");
alertDialogBuilder
.setMessage("Are you sure you want to exit ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
LoginActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
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
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)