BadTokenException 🤖 Android

WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

A dialog or popup was shown with a context whose Activity is finishing/destroyed, or with the Application context.

Seen on: Android

Meaning

Showing dialogs after async work completes, when the user already left the screen, or using applicationContext for dialogs crashes. Check lifecycle state or use DialogFragment.

Common causes

  • Showing a dialog after the Activity finished
  • Using Application context for a Dialog/PopupWindow
  • Showing UI from a background service

⚡ Quick fix

  1. Check !isFinishing && !isDestroyed before showing
  2. Use the Activity context or DialogFragment
  3. Tie async callbacks to lifecycle (lifecycleScope)

Detailed fix by platform

Kotlin

  1. if (!isFinishing && !isDestroyed) dialog.show()

How to diagnose

  1. Context — Activity or Application?
  2. Timing — After async work?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.