0

I have a RuntimeException when I'm trying to open a custom Dialog even though I check if the activity is running.

This is a custom splashscreen and it is triggered on the launching of the app. Everything goes well on the first launch but when my app is into the background and I reopen it, then a crash happens.

I get :

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@e9b1e4 is not valid; is your activity running?                                                                                              
at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.mehcode.reactnative.splashscreen.SplashScreen$1.run(SplashScreen.java:23)

and my code looks like this :

private static Dialog mSplashDialog;

/**
 * Show the splash screen.
 */
public static void show(final Activity activity) {
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (!activity.isFinishing()) {
                mSplashDialog = new Dialog(activity, R.style.RNSplashScreen_SplashTheme);
                mSplashDialog.setCancelable(false);

                if (!mSplashDialog.isShowing()) {
                    mSplashDialog.show();
                }
            }
        }
    });
}

Any ideas of what I am missing?

Thanks in advance for your help :)

  • Probably your dialog is not able to find the context of your activity to show. Try with `if (activity != null)` instead of `if (!activity.isFinishing())`. – SripadRaj Aug 25 '16 at 10:17
  • The Log error message tells you everything. You are trying to show a dialog when the activity's dead or not on top. – Abbas Aug 25 '16 at 10:18
  • Why static? From where you want to open dialog. – Ramit Aug 25 '16 at 10:23
  • @Ramit It's static because it's an external library and i'm calling it from there : `public class MainActivity extends ReactActivity { @Override protected void onCreate(Bundle savedInstanceState) { SplashScreen.show(this);` – Stéphane Maillard Aug 25 '16 at 11:00
  • try using weak reference for mSplashDialog – Ramit Aug 25 '16 at 11:27
  • Do not call dialog in onCreate but later after activity shown http://stackoverflow.com/questions/4187673/problems-creating-a-popup-window-in-android-activity – Ramit Aug 25 '16 at 11:28
  • It works now :) thanks for your help – Stéphane Maillard Aug 25 '16 at 11:43

1 Answers1

0

Adding the permission :

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Solved the issue for React-Native.

William Desportes
  • 1,412
  • 1
  • 22
  • 31