Skip to content

Commit 5256901

Browse files
committed
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 34+ 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.
1 parent a121063 commit 5256901

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

‎test-app/app/src/main/java/com/tns/RuntimeHelper.java‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.tns;
22

3+
import android.app.ActivityManager;
34
import android.app.Application;
45
import android.content.BroadcastReceiver;
6+
import android.content.ComponentName;
57
import android.content.Context;
68
import android.content.Intent;
79
import android.content.IntentFilter;
@@ -25,6 +27,8 @@ private RuntimeHelper() {
2527

2628
private static AndroidJsV8Inspector v8Inspector;
2729

30+
private static final String ERROR_REPORT_ACTIVITY = "com.tns.ErrorReportActivity";
31+
2832
// hasErrorIntent tells you if there was an event (with an uncaught
2933
// exception) raised from ErrorReport
3034
private static boolean hasErrorIntent(Context context) {
@@ -48,7 +52,7 @@ private static boolean hasErrorIntent(Context context) {
4852

4953
if (errFile.exists()) {
5054
errFile.delete();
51-
hasErrorIntent = true;
55+
hasErrorIntent = isErrorActivityStarting(context);
5256
}
5357
}
5458
} catch (Exception e) {
@@ -58,6 +62,35 @@ private static boolean hasErrorIntent(Context context) {
5862
return hasErrorIntent;
5963
}
6064

65+
// The marker outlives a crash whose error activity never started (Android
66+
// can block that start from the background), so it only counts when this
67+
// process is starting for the error activity. Skipping runtime init for
68+
// any other activity leaves it without a runtime.
69+
private static boolean isErrorActivityStarting(Context context) {
70+
try {
71+
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
72+
if (activityManager == null) {
73+
return true;
74+
}
75+
for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
76+
ActivityManager.RecentTaskInfo info = task.getTaskInfo();
77+
ComponentName top;
78+
if (Build.VERSION.SDK_INT >= 23) {
79+
top = info.topActivity;
80+
} else {
81+
top = info.baseIntent != null ? info.baseIntent.getComponent() : null;
82+
}
83+
if (top != null && ERROR_REPORT_ACTIVITY.equals(top.getClassName())) {
84+
return true;
85+
}
86+
}
87+
return false;
88+
} catch (Exception e) {
89+
Log.d(logTag, "Could not tell which activity is starting: " + e.getMessage());
90+
return true;
91+
}
92+
}
93+
6194
public static Runtime initRuntime(Context context) {
6295
if (Runtime.isInitialized()) {
6396
return Runtime.getCurrentRuntime();

0 commit comments

Comments
 (0)