Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a dialog on Currently visible activity on android?

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)
like image 713
Amit Avatar asked Jun 12 '12 10:06

Amit


People also ask

How do you check if the current activity has a dialog in front?

How do you check if the current activity has a dialog in front? You can override activity method onWindowFocusChanged(boolean hasFocus) and track the state of your activity. Normally, if some alert dialog is shown above your activity, the activity does not get onPause() and onResume() events.

How do I show custom dialog?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can I display a list view in an android alert dialog?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.


3 Answers

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.

like image 193
Vineet Shukla Avatar answered Nov 11 '22 08:11

Vineet Shukla


Simply you can create an Activity and set its theme to Dialog in manifest like this :

  <activity
        android:name="Dialog_MsgBox"
        android:launchMode="singleInstance"
        android:theme="@android:style/Theme.Dialog" >
    </activity>

also set launchMode to singleInstance to prevent multiple instance of activity. use whatever layout you want to use for your dialog.

To set different messages, put extra string messages and get them at you Dialog(activity) start up.

like image 28
Vipul Purohit Avatar answered Nov 11 '22 08:11

Vipul Purohit


Not the best solution but it worked for me..

I created an abstract class MyActivity which extends Activity and placed the setter calls in side OnResume and OnPause of this class. All other activities of my Application simply extend this Custom super class instead of Activity.

Then I created a variable of this class in Application class,

private Activity currentOnTopActivity;

I set/reset this variable inside onResume() and onPause() of MyActivity class.

That done, Whenever I want to show a Dialog to the user, I just do following...

   if (currentOnTopActivity!=null && !currentOnTopActivity.isFinishing()) {
                currentOnTopActivity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        String msg = "Some msg";
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(currentOnTopActivity);
                        AlertDialog invitationDialog = null;

                        // set title
                        alertDialogBuilder.setTitle("Title ");

                        // set dialog message
                        alertDialogBuilder.setMessage(msg).setCancelable(false).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                         // do something                            }
                        });

                        // create alert dialog
                        invitationDialog = alertDialogBuilder.create();

                        // show it on UI Thread
                        invitationDialog.show();

                    }

                });
            }

Intdroducing a Super Activity class also gives me the flexibility to place common code in this abstract class instead of duplicating that in every other Activity.

like image 2
Amit Avatar answered Nov 11 '22 08:11

Amit