How can I display the "Current Visible Activity on Android" dialog box?

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);

        // 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)
+7
3

, :

1. Activity transparent theme and no title.

2. onCreate() alert dialog.

3. broadcastReceiver alert dialog.

+6

Activity Dialog manifest :

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

launchMode singleInstance, . , .

, ().

+4

, ..

MyActivity, Activity, OnResume OnPause . Activity.

Application,

private Activity currentOnTopActivity;

/ onResume() onPause() MyActivity.

This is done when I want to show the dialog to the user, I just do the 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();

                    }

                });
            }

Introducing the Super Activity class also gives me the opportunity to put common code in this abstract class, instead of duplicating it in any other Activity.

+2
source

All Articles