Skip to content

[@capacitor/splash-screen] Android: hide() removes the launch pre-draw listener off the UI thread, crashing cold start #2627

Open

Description

@puchtac

Plugin: @capacitor/splash-screen 8.0.2 (Android)

Describe the bug

SplashScreen.hide() runs on Capacitor's plugin thread. On the Android 12 splash-screen API path it calls
content.getViewTreeObserver().removeOnPreDrawListener(this.onPreDrawListener) directly on that thread, but a
ViewTreeObserver may only be touched from the UI thread.

With launchAutoHide: false the launch listener returns false, so it holds drawing and the UI thread runs
ViewTreeObserver.dispatchOnPreDraw() on every frame until the web layer calls hide(). The off-thread removal
then races that dispatch: removing an entry shifts the array under the dispatch loop and leaves a null slot, and
the app dies with

java.lang.NullPointerException: Attempt to invoke interface method
'boolean android.view.ViewTreeObserver$OnPreDrawListener.onPreDraw()' on a null object reference
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1116)

roughly 10 ms after the hide() call. The same block also sets isVisible = false before the removal, so a
dispatch landing between the two can set it back and leave the splash pinned up.

Both affected call sites are in android/src/main/java/com/capacitorjs/plugins/splashscreen/SplashScreen.java:
the isLaunchSplash early-return inside hide(int, boolean), and the "Hide with Android 12 API" branch of
hideDialog(...).

To Reproduce

  1. A Capacitor 8 Android app targeting the Android 12 splash API, with launchAutoHide: false in
    capacitor.config, so the launch splash is held until the web layer hides it.
  2. Call SplashScreen.hide() from JavaScript as soon as the web app has painted (the usual pattern).
  3. Cold-start the app repeatedly on a device or emulator (Android 12+; seen on Android 14).

It is a race, so it is intermittent — observed twice in one night across an automated relaunch suite that
cold-starts the app many times.

Expected behavior

Hiding the launch splash should never crash the app, and the pre-draw listener should be removed on the UI thread.

Suggested fix

Post the whole release — clear isVisible, remove the listener, clear the field — to the main looper as a single
runnable, from both call sites. Doing all three together on the UI thread also closes the second half of the bug,
since no dispatch can run between clearing the flag and removing the listener:

private void releaseLaunchSplash(final OnPreDrawListener listener) {
    new Handler(context.getMainLooper()).post(() -> {
        isVisible = false;
        if (null != content) {
            content.getViewTreeObserver().removeOnPreDrawListener(listener);
        }
        if (onPreDrawListener == listener) {
            onPreDrawListener = null;
        }
    });
}

We are running exactly this as a pinned patch against 8.0.2 and the cold-start crash has not recurred. Happy to
open a PR if that is useful.

Additional context

  • Capacitor: 8.4.2
  • @capacitor/splash-screen: 8.0.2 (current npm latest at time of writing)
  • Platform: Android 12+ (reproduced on Android 14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions