Having threads to do some heavy lifting and long processing in the background is pretty standard stuff. Very often you would want to notify or prompt the user after the background task has finished by displaying a Dialog.

The displaying of the Dialog has to happen on the UI thread, so you would do that either in the Handler object for the thread or in the onPostExecute method of an AsyncTask (which is a thread as well, just an easier way of implementing it). That is a textbook way of doing this and you would think that pretty much nothing wrong could go with this.

Surprisingly I found out that something CAN actually go wrong with this. After Google updated the Android Market and started giving crash reports to the developers I received the following exception:

android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@447a6748 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:468)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:239)
at android.app.Activity.showDialog(Activity.java:2488)

at android.os.Handler.dispatchMessage(Handler.java:99)

I only got a couple of these exceptions from thousands of installs, so I knew that was not anything that happens regularly or that it was easy to replicate.

Looking at the stack trace above it gives us a pretty good idea why it failed. It started in the Handler object, which naturally was called by a background thread after it finished its processing. The Handler instance tried to show a Dialog and before it could show it, it tried to set the View for it and then it failed with:

android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@447a6748 is not valid; is your activity running?

The 447a6748 number is just a memory address of an object that no longer exists.

Note- do not get hung up on the exact number. It would be different with every execution.

Now we know why the application crashed, the only thing left is to figure out what caused it?

We know that background threads execute independently of the main UI thread. That means that the user could be interacting with the application during the time that the thread is doing its work under the covers. Well, what happens if the user hits the “Back” button on the device while the background thread is running and what happens to the Dialog that this thread is supposed to show? Well, if the timing is right the application will most likely crash with the above described error.

In other words what happens is that the Activity will be going through its destruction when the background thread finishes its work and tries to show a Dialog.

In this case it is almost certain that this should have been handled by the Virtual Machine. It should have recognized the fact that the Activity is in the process of finishing and not even attempted to show the Dialog. This is an oversight of the Google developers and it will probably be fixed some time in the future, but in the meantime the burden is on us to take care of this.

The fix to this is pretty simple. Just test if the Activity is going through its finishing phase before displaying the Dialog:

private Handler myHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    switch (msg.what) {
      case DISPLAY_DLG:
        if (!isFinishing()) {
        showDialog(MY_DIALOG);
        }
      break;
    }
  }
};
Android – Displaying Dialogs From Background Threads

1,582 thoughts on “Android – Displaying Dialogs From Background Threads

  • September 30, 2025 at 1:30 am
    Permalink

    ????? ????????? ????????????????, ?? ?????????????? ??????????? ????????? — ??? ???????????????? ???????????? ? «??????????» ????? ?????????????.
    ????????? – narkologicheskaya-klinika

  • September 30, 2025 at 2:03 am
    Permalink

    ?? ???????? ??? ?????? ??????????, ????????? ? ?????????, ? ???????????? ?? ?????????? ????????. ?????? ? ??? — ??? ?? «???? ??????????», ? ??????? ???: ???????????? ? ????????? ????????????, ?????? ?????? ? ???, ?????? ?????????? ????????? ?? ??????????, ???????? ????????/??????/?????????/???????????, ?????? ??? ? ?????? ???????. ????? ???????????? ????????? ????????? ????????? ??????????: ?????????? ? ??????????? (???? ????????), ????????? ?????????????? ??????? ? ???????????????? ????.
    ???????? ?????????????? ?????????? – narkologicheskaya-klinika-pomoshch-na-domu

  • September 30, 2025 at 2:43 am
    Permalink

    ?????????? ?????????? ???????? ?? ?????? ??????????, ? ??????????? ??? ??????? ? ????????????. ?? ??????????? ??????????? ????????, ????????? ????????? ????? ?????? ? ???????? ? ???????????????? ?????? ???????.
    ?????? ????? > – https://www.mdpiscines.fr/fond-piscines

  • September 30, 2025 at 4:06 am
    Permalink

    ?????? ???????? ? ?????????? ?????????, ??????? ???????????? ???? ????????. ?? ??????? ??????????? ?? ?????????? ??????????? — ?? ???????, ??? ??????? ?????? ?? ???? ????????, ?????????? ? ????? ?????.
    ??????? ? ?????? – http://aneja.org/aneja

  • September 30, 2025 at 4:32 am
    Permalink

    ??? ???????? ??????? ???????? ???????? ??????? ? ????? ?? ?????????? ????????. ??? ??????? ????????? ?????? ??????????????? ? ???? ? ?????? ? ????? ?????? ???????? ???????. ???????? ??????? ???? ?? ??????????? ?????????? ? ??????????? ? ????? ???????!
    ?????? ?? ????? – https://www.funclickgames.com/hello-world

  • September 30, 2025 at 4:34 am
    Permalink

    ???? ????????????? ????? ?????????? ??????????????? ??????????? ? ??????????? ???????. ?? ?????????? ????????? ????????? ?? ????????? ???? ??? ????? ?????, ???????????? ?????????? ? ????????? ????????. ???????? ???????????? ?? ?????? ? ????????? ????????!
    ??????????? ???????? ?????? – https://moroccoextratours.com/hello-world

  • September 30, 2025 at 4:49 am
    Permalink

    ? ???? ??????-?????? ?? ??????? ?????????? ?????????? ? ?????????? ?????, ??????? ???????? ?????? ????. ???????? ?????? ???????????? ? ?????????? ???????? ? ?????????, ??? ???????? ?? ????????? ???????? ? ?????? ?????? ??????????? ???????.
    ???????? ?????? ?????????? – https://studentssolution.com.pk/left-sidebar

  • September 30, 2025 at 4:55 am
    Permalink

    ??? ?????? ?????????? ????????????? ? ???????? ???????, ??????? ????????? ???????? ???????? ????? ?????????. ?? ??????????? ??????????? ???? ????, ??????? ????????? ??? ?? ????????? ? ????? ? ??????????? ???????????? ??????? ??? ???????????? ????????. ??????? ? ??????????????!
    ???, ??? ????? ????? – https://www.sqigroup.com/the-humble-beginnings

  • September 30, 2025 at 4:56 am
    Permalink

    ?????????? ?????????? ?????????? ???????? ??????????, ??????? ????? ????????? ??? ????????????, ??? ? ???????? ????? ?????????. ????? ?? ??????? ?????? ?? ????? ?????????? ??????? ? ???????? ??????? ??? ??????????? ??????????.
    ?????????? ??? – https://peruestor.com/hello-world

  • September 30, 2025 at 5:07 am
    Permalink

    ??? ?????????? ????????? ??? ? ??? ????????????? ?????? ? ???????????? ????????. ?? ????????? ? ???????? ????????, ??????? ???????? ??? ???????, ? ?????????? ?????? ??? ???????? ????????????, ??????? ?????????? ????????. ???????, ???? ????? ??????? ??? ??????? ? ??? ????????? ??? ?????? ? ???????.
    ???, ??? ????? ????? – https://www.casagaro.com.ar/web/blogging-2/left-sidebar-post

  • September 30, 2025 at 5:08 am
    Permalink

    ??? ?????????????? ?????? ???????? ???????? ?????, ?????? ? ????????????, ??????? ??????? ??? ???? ? ????? ????????? ????????? ? ????????? ? ????????? ???????. ???????? ????????? ???, ????? ???? ???????? ? ???????? ???????.
    ????????? ?????????????? ?????? – http://www.elena-psyholog.ru

  • September 30, 2025 at 6:13 am
    Permalink

    ?? ??????? ??? ??? ????? ????????????? ????? ?? ???? ????? ? ???????. ?? ???????????? ??????? ?? ??????????? ??????? — ??? ?????? ???????? ??? ???????? ? ??????? ????? ????????? ????, ??? ??????? ??? ???.
    ??????? ?????? ???????????? – https://shukorani.com/portfolio/so-5

  • September 30, 2025 at 6:20 am
    Permalink

    ??? ???????? ??????? ???????? ???????? ??????? ? ????? ?? ?????????? ????????. ??? ??????? ????????? ?????? ??????????????? ? ???? ? ?????? ? ????? ?????? ???????? ???????. ???????? ??????? ???? ?? ??????????? ?????????? ? ??????????? ? ????? ???????!
    ????? ???? — ??????? ????? ??????????? – https://template110.webekspor.com/?p=78

  • September 30, 2025 at 6:32 am
    Permalink

    ??? ?????????? ????????? ??? ? ??? ????????????? ?????? ? ???????????? ????????. ?? ????????? ? ???????? ????????, ??????? ???????? ??? ???????, ? ?????????? ?????? ??? ???????? ????????????, ??????? ?????????? ????????. ???????, ???? ????? ??????? ??? ??????? ? ??? ????????? ??? ?????? ? ???????.
    ????????? ??? – https://appeareal.com/smooth-and-shiny-nails-top-uses-of-nail-buffer

  • September 30, 2025 at 10:24 am
    Permalink

    Do you have a spam problem on this blog; I also am a blogger, and I was wondering your situation; many of us have developed some nice practices and we are looking to exchange strategies with others, be sure to shoot me an e-mail if interested.

    Vegastars casino

  • September 30, 2025 at 1:02 pm
    Permalink

    Unlock your images with WebPtoJPGHero.com, the simple, free solution for solving WebP compatibility issues. While the WebP format helps websites load faster, it can leave you with files you can’t easily edit or share. Our online tool provides an instant fix, transforming any WebP image into a high-quality JPG that works seamlessly across all your devices and applications. You can fine-tune the output quality to balance clarity and file size, and even save time by converting an entire batch of photos at once. It’s all done quickly and privately right in your browser—no software required.
    WebPtoJPGHero.com

  • September 30, 2025 at 1:59 pm
    Permalink

    ???? ?????? ? ???????-??-???? ???????? ?? ?????? ?????????? ????????????, ?? ? ??????????????? ????????? ??? ????? ???????????? ??????????????.
    ????????? ????? ?????? ??? – ????? ?? ????? ?? ???? ???? ? ???????-??-????

  • September 30, 2025 at 2:02 pm
    Permalink

    ?? ?????? ??????? ?????? ???????????????: ??????? ?????? ????????, ???????? ?????????? ? ???????? ???? ?? ????????? ?????. ???????? ???? ???????? ?????? ??, ??? ?????? ?? ???????????? — ???????????? ???????, ?????????? ????????? ? ?????????????, ??? ? ??????, ??????? ????????? ????. ?? ????? ??????? ????????? ???????: ????? ?? ???, ????? ??? ???????? ??? ????????? ? ??????????? 24/7. ?????????????????? ???????? ?? ?????????: ??????????? ????????????, ?????? ? ????? ?????? ? ??????????? ???????, ????????? ?? ??????? — ? ??????????? ?????????????. ??????? ??? ?????, ???????? ????????? ?? ????: ?????? ???????????? ??? «???????» ? ? ????????? ???????????? ???????.
    ??????? ?????? ?????? – https://narkologicheskaya-klinika-domodedovo0.ru/

  • September 30, 2025 at 2:49 pm
    Permalink

    JPG Hero converter is an online image converter built for speed, simplicity, and accessibility. This web-based platform allows instant conversion of popular image formats such as PNG, WebP, and BMP into JPG files. No downloads, installations, or user accounts are required. The entire process happens in-browser and takes only a few seconds. Users can upload up to 20 files at once for batch conversion, making it suitable for individuals and professionals managing multiple images. All uploaded files are automatically deleted after processing to maintain privacy and ensure data security. JPGHero.com is compatible with all modern devices, including desktops, tablets, and smartphones, offering flexibility for work on the go. Whether the goal is to reduce image file size, prepare visuals for the web, or standardize a project’s image format, this tool delivers clean, reliable results without any extra steps.
    JPGHero

  • September 30, 2025 at 2:57 pm
    Permalink

    ???? ??? ??? ????? ??????? ????????? ????? ?? ????? ? ???????-??-????, ??????? «????1» ????????????? ????????????????? ??????. ????? ??????? ?? ??? ??? ?? ??????? ?????? ??????? ? ??????????. ???? ?? ?????? ?????????? ?? 3500 ??????.
    ?????? ?????? – vyvod-iz-zapoya-rostov25.ru

  • September 30, 2025 at 3:58 pm
    Permalink

    ???? ??????????? ? ???????-??-???? ????????? ??????? ? ?????????? ?????????????? ????? ??????????? ???????????? ????????, ????????? ?????????????? ????? ???????.
    ??????????? ?????? ????????? – ????? ????????? ?? ??? ? ???????-??-????

  • September 30, 2025 at 4:15 pm
    Permalink

    ?? ?????? ??????? ?????? ???????????????: ??????? ?????? ????????, ???????? ?????????? ? ???????? ???? ?? ????????? ?????. ???????? ???? ???????? ?????? ??, ??? ?????? ?? ???????????? — ???????????? ???????, ?????????? ????????? ? ?????????????, ??? ? ??????, ??????? ????????? ????. ?? ????? ??????? ????????? ???????: ????? ?? ???, ????? ??? ???????? ??? ????????? ? ??????????? 24/7. ?????????????????? ???????? ?? ?????????: ??????????? ????????????, ?????? ? ????? ?????? ? ??????????? ???????, ????????? ?? ??????? — ? ??????????? ?????????????. ??????? ??? ?????, ???????? ????????? ?? ????: ?????? ???????????? ??? «???????» ? ? ????????? ???????????? ???????.
    ????????? ??? – ??????? ??????????????? ??????

  • September 30, 2025 at 4:15 pm
    Permalink

    JPG Hero converter is an online image converter built for speed, simplicity, and accessibility. This web-based platform allows instant conversion of popular image formats such as PNG, WebP, and BMP into JPG files. No downloads, installations, or user accounts are required. The entire process happens in-browser and takes only a few seconds. Users can upload up to 20 files at once for batch conversion, making it suitable for individuals and professionals managing multiple images. All uploaded files are automatically deleted after processing to maintain privacy and ensure data security. JPGHero.com is compatible with all modern devices, including desktops, tablets, and smartphones, offering flexibility for work on the go. Whether the goal is to reduce image file size, prepare visuals for the web, or standardize a project’s image format, this tool delivers clean, reliable results without any extra steps.
    JPGHero

  • September 30, 2025 at 4:35 pm
    Permalink

    ???? ?? ??? ???? ??????? ?????????? ? ?????? ?? ????? ? ???????-??-????, ??????? «????1» ?????????? ????????????????? ??????. ????? ??????? ?? ??? ??? ?? ??????? ?????? ??????? ? ??????????. ???? ?? ?????? ?????????? ?? 3500 ??????.
    ????????? – ????? ????? ????????? ?? ??? ? ???????-??-????

Leave a Reply

Your email address will not be published. Required fields are marked *

*