Repository navigation
feat(android): [Part 4] Prefer Activity ui.load transactions over Nav3 transactions #6484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
0xadam-brown
merged 4 commits into
feat/nav3-lifecycle
from
feat/nav3-lifecycle-4-ui-load-preference
Nov 13, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
011d18c
feat(android): Prefer an Activity's ui.load transaction over Nav3 tra…
0xadam-brown 2c7ae88
changelog
0xadam-brown f1dd336
ref(android): Address Nav3 lifecycle review feedback
0xadam-brown 9061702
ref(android): Measure the Nav3 ui.load retry window with a Deadline
0xadam-brown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,22 +3,59 @@ package io.sentry.compose.navigation3.lifecycle | |
| import android.app.Activity | ||
| import android.app.Application | ||
| import android.app.Application.ActivityLifecycleCallbacks | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
|
|
||
| /** | ||
| * Watches every Activity of the host application and reports the Activity lifecycle events that | ||
| * [NavLeaseCoordinator] acts on. | ||
| * [NavLeaseCoordinator] acts on, so that the coordinator needn't know how they differ across API | ||
| * levels. | ||
| * | ||
| * - [onActivityResumed] is called when any Activity resumes, including Activities without a | ||
| * `SentryNavEffect`. | ||
| * Each event is reported relative to `ActivityLifecycleIntegration`, which binds an Activity's | ||
| * `ui.load` transaction to the scope when the Activity is created and removes it when the Activity | ||
| * is destroyed: | ||
| * | ||
| * - [onActivityCreatedOrResumed] is called when an Activity is created and again when it resumes, | ||
| * with [ActivityEvent.CREATED] or [ActivityEvent.RESUMED] as its event. On API 29+, creation is | ||
| * reported before `ActivityLifecycleIntegration` tries to bind the Activity's `ui.load` | ||
| * transaction; below API 29, after. So below API 29, a withdrawal on creation comes too late for | ||
| * that `ui.load` transaction, which stays unbound (see "Non-candidate roots and lease withdrawal" | ||
| * in the package README). | ||
| * | ||
| * - [onActivityDestroyed] is called when an Activity is destroyed, after | ||
| * `ActivityLifecycleIntegration` has removed the Activity's `ui.load` transaction from the scope. | ||
| * | ||
| * **Android ordering assumptions** | ||
| * | ||
| * The watcher relies on the following callback orders, which Android doesn't document but AOSP has | ||
| * historically upheld: | ||
| * | ||
| * - `Application` calls `ActivityLifecycleCallbacks` in registration order. Below API 29, | ||
| * `ActivityLifecycleIntegration`'s callbacks run before this class's, provided the SDK is | ||
| * initialized before this class starts [watching][watch]. | ||
| * | ||
| * - On API 29+, every `onActivityPreCreated` callback runs before any `onActivityCreated` callback, | ||
| * and every `onActivityDestroyed` callback before any `onActivityPostDestroyed` callback. | ||
| * | ||
| * **Lifetime** | ||
| * | ||
| * The callbacks are registered at most once per watcher and never unregistered. Like | ||
| * `defaultNavLeaseCoordinator`, which owns the watcher used in practice, they're meant to live as | ||
| * long as the process. | ||
| */ | ||
| internal class NavActivityWatcher(private val onActivityResumed: (activity: Activity) -> Unit) { | ||
| internal class NavActivityWatcher( | ||
| private val onActivityCreatedOrResumed: (activity: Activity, event: ActivityEvent) -> Unit, | ||
| private val onActivityDestroyed: (activity: Activity) -> Unit, | ||
| ) { | ||
|
|
||
| /** | ||
| * The Activity lifecycle event passed to [onActivityCreatedOrResumed], described by [description] | ||
| * in past tense (e.g., "created") for log messages. | ||
| */ | ||
| enum class ActivityEvent(val description: String) { | ||
| CREATED("created"), | ||
| RESUMED("resumed"), | ||
| } | ||
|
|
||
| private var isWatching = false | ||
|
|
||
|
|
@@ -33,11 +70,23 @@ internal class NavActivityWatcher(private val onActivityResumed: (activity: Acti | |
|
|
||
| private inner class Callbacks : ActivityLifecycleCallbacks { | ||
|
|
||
| override fun onActivityResumed(activity: Activity) { | ||
| [email protected](activity) | ||
| // Only called on API 29+, before any callback's onActivityCreated. | ||
| override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) { | ||
| onActivityCreatedOrResumed(activity, ActivityEvent.CREATED) | ||
| } | ||
|
|
||
| // Covers API levels below 29, as they don't call onActivityPreCreated. Runs after | ||
| // ActivityLifecycleIntegration's onActivityCreated, which finds the slot taken and never binds | ||
| // the Activity's ui.load transaction. This is a known limitation (see the package README). | ||
| override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { | ||
| onActivityCreatedOrResumed(activity, ActivityEvent.CREATED) | ||
| } | ||
| } | ||
|
|
||
| override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit | ||
| override fun onActivityResumed(activity: Activity) { | ||
| onActivityCreatedOrResumed(activity, ActivityEvent.RESUMED) | ||
| } | ||
|
|
||
| override fun onActivityStarted(activity: Activity) = Unit | ||
|
|
||
|
|
@@ -47,6 +96,16 @@ internal class NavActivityWatcher(private val onActivityResumed: (activity: Acti | |
|
|
||
| override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit | ||
|
|
||
| override fun onActivityDestroyed(activity: Activity) = Unit | ||
| // Covers API levels below 29, which don't call onActivityPostDestroyed. | ||
| override fun onActivityDestroyed(activity: Activity) { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { | ||
| [email protected](activity) | ||
| } | ||
| } | ||
|
|
||
| // Only called on API 29+, after every callback's onActivityDestroyed. | ||
| override fun onActivityPostDestroyed(activity: Activity) { | ||
| [email protected](activity) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.