Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:31:14+00:00 2026-06-05T14:31:14+00:00

My Problem is similar to this 2 year old question , I am just

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:31:16+00:00Added an answer on June 5, 2026 at 2:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this question has bothered me on and off for about a year, and I
Sorry if this has been asked before - there's a question with a similar
I have a problem similar to this question: Wicket: can Checkgroup be Ajax enabled?
A similar question was asked and answered about a year ago, but was either
Hey, I have a problem similar to this one ( click ). Approx 2
i am using apachebench (ab) to test a web server this problem is similar
I have a problem similar to this one: How can I execute code after
I’m having a setTimeout problem similar to this one . But that solution doesn't
My problem is similar to this one, How to update a widget every minute
I have a similar problem.This is a snippet of my source: FileWriter fstream =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.