Skip to content

Commit 3c24dd4

Browse files
authored
Weakly reference Activity for transaction finished callback (#2203)
1 parent 4eb446a commit 3c24dd4

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Prevent NPE by checking SentryTracer.timer for null again inside synchronized ([#2200](https://github.com/getsentry/sentry-java/pull/2200))
8+
- Weakly reference Activity for transaction finished callback ([#2203](https://github.com/getsentry/sentry-java/pull/2203))
89
- `attach-screenshot` set on Manual init. didn't work ([#2186](https://github.com/getsentry/sentry-java/pull/2186))
910
- Remove extra space from `spring.factories` causing issues in old versions of Spring Boot ([#2181](https://github.com/getsentry/sentry-java/pull/2181))
1011

sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.sentry.util.Objects;
2424
import java.io.Closeable;
2525
import java.io.IOException;
26+
import java.lang.ref.WeakReference;
2627
import java.util.Date;
2728
import java.util.List;
2829
import java.util.Map;
@@ -146,6 +147,7 @@ private void stopPreviousTransactions() {
146147
}
147148

148149
private void startTracing(final @NotNull Activity activity) {
150+
WeakReference<Activity> weakActivity = new WeakReference<>(activity);
149151
if (performanceEnabled && !isRunningTransaction(activity) && hub != null) {
150152
// as we allow a single transaction running on the bound Scope, we finish the previous ones
151153
stopPreviousTransactions();
@@ -167,7 +169,20 @@ private void startTracing(final @NotNull Activity activity) {
167169
(Date) null,
168170
true,
169171
(finishingTransaction) -> {
170-
activityFramesTracker.setMetrics(activity, finishingTransaction.getEventId());
172+
@Nullable Activity unwrappedActivity = weakActivity.get();
173+
if (unwrappedActivity != null) {
174+
activityFramesTracker.setMetrics(
175+
unwrappedActivity, finishingTransaction.getEventId());
176+
} else {
177+
if (options != null) {
178+
options
179+
.getLogger()
180+
.log(
181+
SentryLevel.WARNING,
182+
"Unable to track activity frames as the Activity %s has been destroyed.",
183+
activityName);
184+
}
185+
}
171186
});
172187
} else {
173188
// start transaction with app start timestamp
@@ -178,7 +193,20 @@ private void startTracing(final @NotNull Activity activity) {
178193
appStartTime,
179194
true,
180195
(finishingTransaction) -> {
181-
activityFramesTracker.setMetrics(activity, finishingTransaction.getEventId());
196+
@Nullable Activity unwrappedActivity = weakActivity.get();
197+
if (unwrappedActivity != null) {
198+
activityFramesTracker.setMetrics(
199+
unwrappedActivity, finishingTransaction.getEventId());
200+
} else {
201+
if (options != null) {
202+
options
203+
.getLogger()
204+
.log(
205+
SentryLevel.WARNING,
206+
"Unable to track activity frames as the Activity %s has been destroyed.",
207+
activityName);
208+
}
209+
}
182210
});
183211
// start specific span for app start
184212

0 commit comments

Comments
 (0)