From 165aff29b0ef316ae93ab73a333f8f31fa23bb92 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 09:39:43 -0300 Subject: [PATCH] fix(runtime): ignore a stale error-report marker ErrorReport writes files/hasError before it starts ErrorReportActivity, and the next process start consumes it to skip runtime init so the error screen can show. When Android blocks the error activity's start (a crash during Application.onCreate on API 35 is a background activity launch), the marker survives, and the next normal launch skips runtime init and crashes. The marker now counts only when one of the app's tasks has ErrorReportActivity on top, i.e. the process is starting for it. --- .../src/main/java/com/tns/RuntimeHelper.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test-app/app/src/main/java/com/tns/RuntimeHelper.java b/test-app/app/src/main/java/com/tns/RuntimeHelper.java index fa1542966..28a0610bc 100644 --- a/test-app/app/src/main/java/com/tns/RuntimeHelper.java +++ b/test-app/app/src/main/java/com/tns/RuntimeHelper.java @@ -1,7 +1,9 @@ package com.tns; +import android.app.ActivityManager; import android.app.Application; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -25,6 +27,8 @@ private RuntimeHelper() { private static AndroidJsV8Inspector v8Inspector; + private static final String ERROR_REPORT_ACTIVITY = "com.tns.ErrorReportActivity"; + // hasErrorIntent tells you if there was an event (with an uncaught // exception) raised from ErrorReport private static boolean hasErrorIntent(Context context) { @@ -48,7 +52,7 @@ private static boolean hasErrorIntent(Context context) { if (errFile.exists()) { errFile.delete(); - hasErrorIntent = true; + hasErrorIntent = isErrorActivityStarting(context); } } } catch (Exception e) { @@ -58,6 +62,35 @@ private static boolean hasErrorIntent(Context context) { return hasErrorIntent; } + // The marker outlives a crash whose error activity never started (Android + // can block that start from the background), so it only counts when this + // process is starting for the error activity. Skipping runtime init for + // any other activity leaves it without a runtime. + private static boolean isErrorActivityStarting(Context context) { + try { + ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + if (activityManager == null) { + return true; + } + for (ActivityManager.AppTask task : activityManager.getAppTasks()) { + ActivityManager.RecentTaskInfo info = task.getTaskInfo(); + ComponentName top; + if (Build.VERSION.SDK_INT >= 23) { + top = info.topActivity; + } else { + top = info.baseIntent != null ? info.baseIntent.getComponent() : null; + } + if (top != null && ERROR_REPORT_ACTIVITY.equals(top.getClassName())) { + return true; + } + } + return false; + } catch (Exception e) { + Log.d(logTag, "Could not tell which activity is starting: " + e.getMessage()); + return true; + } + } + public static Runtime initRuntime(Context context) { if (Runtime.isInitialized()) { return Runtime.getCurrentRuntime();