How can I display a dialog on Currently visible activity on android?
android, android-activity, android-dialog
Solution
Try this if it helps you:
1. Create a `Activity` with `transparent theme and no title`.
2. In `onCreate()` define your `alert dialog`.
3. Starting this activity from broadcastReceiver will show the `alert dialog`.
Problem
My Problem is similar to this 2 year old question, I am just re posting the same problem to get the updated answers, since a lot has changed in two years. I am developing for an app for GingerBread+ devices, I have many activities and in background I receive some data from the server. Now based on that data in some cases I need to show a `Dialog` to the user. Problem is How do I know which the current front most activity ? What I tried, I have tried giving the `getApplicationContext()` while `Dialog` creation, however that is not working. Throwing some exception. A solution ? (I really hate it), A solution could be to keep track of the currently visible activity by having a variable in `Application` class, and setting it on `onResume()` of each activity. I really don't want to do this book keeping if their are smarter ways to achieve this and I am sure their are smarter ways to achieve this, My simple question is, How can I display a dialog on Currently visible activity ?, So that I can give that reference to the AlertDialog.Builder, which I think will do my job.. If not than How I can display a dialog on topmost Activity ? Edit, I create a simple dialog using following code 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"); } }; ``` Exception using AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getApplicationContext()); gives me following exception, ``` 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) ```