返回提示词目录第 1350 页

安卓界面状态与生命周期审计

针对安卓应用的组合式界面实现,追踪状态、副作用及导航缺陷,给出复现与修复依据。

完整提示词

I want you to perform a maximally deep, systematic, evidence-first, and runtime-oriented Jetpack Compose implementation analysis of the entire Android application.

Primary objective:

> Determine whether the Compose UI properly manages state, recomposition, lifecycle, side effects, navigation, performance, and accessibility semantics, without hidden race conditions, stale state, memory leaks, or production-only issues.

This is not:

- a generic Compose best practices checklist
- a recommendation to rewrite everything in Compose
- an automated refactor
- a superficial naming check
- a witch hunt for every single recomposition
- an attempt to blindly sprinkle `remember`, `derivedStateOf`, or `key` everywhere
- a shallow analysis of just one screen

The focus is on the actual behavior of the Compose runtime.

Priority:

**correctness > lifecycle safety > state consistency > side-effect safety > performance > architecture elegance**

Do not report an issue solely because an alternative Compose pattern exists.

Every serious finding must have a concrete execution flow and a tangible consequence.

---

# 1. DETERMINE COMPOSE STACK

Prior to analysis, identify:

- Kotlin version
- Android Gradle Plugin
- Compose Compiler or relevant compiler configuration
- Compose BOM
- Compose UI
- Material / Material3
- Navigation Compose
- Lifecycle Compose
- Activity Compose
- Paging Compose
- Coil Compose or other image loading stack
- Hilt Navigation Compose if present
- Testing libraries
- minSdk
- targetSdk

Inspect:

- Gradle configuration
- Version catalog
- Compose compiler options
- Feature flags
- Stability configuration if present
- ProGuard/R8 rules
- Release build setup

Do not rely on outdated Compose assumptions.

If a conclusion depends on library version or compiler behavior, first verify the actual version.

---

# 2. MAP COMPOSE UI ARCHITECTURE

Before filing findings, map out:

```text
Activity
↓
Root Composable
↓
Navigation Host
↓
Screen Composable
↓
ViewModel
↓
StateFlow
↓
UI State
↓
Reusable Components
```

If the app uses a different model, document the actual model.

Identify:

- root composition
- navigation boundaries
- screen composables
- reusable UI
- state holders
- ViewModels
- custom hooks-like composables/helpers
- effect-heavy composables
- local persistence
- event system

---

# 3. STATE INVENTORY

Map all critical state sources:

- `remember`
- `rememberSaveable`
- `mutableStateOf`
- `derivedStateOf`
- `StateFlow`
- `SharedFlow`
- LiveData
- ViewModel state
- SavedStateHandle
- Room Flow
- DataStore Flow
- navigation arguments

For each critical piece of state, ask:

> Who is the actual owner of this state?

---

# 4. SINGLE SOURCE OF TRUTH

Look for duplicate copies of identical data across multiple places.

Example:

```text
ViewModel StateFlow
↓
collected state
↓
copied into local remember state
↓
form state
```

Verify whether state drift can occur.

Do not report local UI state simply because it exists.

A problem arises when multiple independent copies represent the exact same business truth.

---

# 5. `remember`

For each critical `remember`, check:

- what is remembered
- how long it is intended to live
- what it depends on
- whether keys accurately reflect semantics

Look for:

```kotlin
remember { expensiveObject(id) }
```

if `id` can change while the object should be recreated.

---

# 6. `remember` KEY BUGS

Scenario:

```text
Composable rendered for item A
↓
remember caches object A
↓
same composable instance receives item B
↓
remember has no key
↓
state/object still belongs to A
```

This represents an actual correctness finding.

---

# 7. `rememberSaveable`

Verify whether state that must survive recreation uses an appropriate persistence model.

Do not use `rememberSaveable` for:

- large objects
- non-serializable state
- data owned by ViewModel or database

---

# 8. SAVEABLE STATE SIZE

Look for oversized structures in saved state:

- lists
- bitmaps
- entire entity domain objects
- large JSON payloads

Assess the risk of TransactionTooLargeException or excessive Bundle size.

---

# 9. STATE HOISTING

For reusable composables, verify whether state can be controlled externally where appropriate.

However, do not convert every single component into a stateless component.

Ask:

> Does the current ownership hinder reuse, testing, or state synchronization?

---

# 10. CONTROLLED VS INTERNAL STATE

If a composable accepts both:

```kotlin
value
```

parameter and maintains its own internal `remember` state for the same concept, verify who holds authority.

Look for conflicting dual sources of truth.

---

# 11. DERIVED STATE

If a value can be computed from existing Compose state, check whether it is unnecessarily stored as a separate mutable state.

Example:

```text
items
filter
↓
filteredItems
```

If `filteredItems` requires manual synchronization, state drift risk exists.

---

# 12. `derivedStateOf`

Do not add `derivedStateOf` automatically.

Use it only when:

- the computation depends on observable state
- recomposition frequency justifies it
- there is a measurable or tangible benefit

---

# 13. SNAPSHOT STATE

Verify direct mutation of structures inside Compose state.

Example:

```kotlin
val list = remember { mutableListOf<Item>() }
```

where UI expects automatic recomposition upon mutating a standard collection.

---

# 14. SNAPSHOT STATE LIST / MAP

If `mutableStateListOf` or similar types are used, inspect:

- item mutation
- item replacement
- derived rendering

Mutating an internal property of an item that is not observable itself leaves the UI stale.

---

# 15. IMMUTABILITY

Do not demand immutable models purely for stylistic reasons.

Report an issue when a mutable shared model causes:

- missed recomposition
- race conditions
- untraceable state divergence

---

# 16. VIEWMODEL STATE

For each screen ViewModel, map:

```text
repository
↓
Flow
↓
ViewModel
↓
UiState
↓
Composable
```

Look for:

- state fragmented across multiple uncoordinated Flows
- UI forced to combine numerous unrelated streams
- partial state inconsistencies

---

# 17. UI STATE MODEL

Evaluate whether the UI state permits invalid combinations.

Example:

```kotlin
isLoading = true
error = "..."
data = nonNull
```

if these states are mutually contradictory for that feature.

Consider sealed or discriminated models only where they tangibly eliminate ambiguity.

---

# 18. `collectAsState`

Review all collection points.

Check:

- lifecycle awareness
- unnecessary always-on collection
- multiple redundant collections of the same Flow

---

# 19. `collectAsStateWithLifecycle`

If used, verify lifecycle behavior and dependency version.

If not used, do not automatically report an issue until you establish that collection actually executes outside the necessary active lifecycle.

---

# 20. DUPLICATE FLOW COLLECTION

The same Flow might be collected in multiple composables.

This may be benign for hot Flows, but costly or hazardous for cold Flows.

Determine upstream semantics.

---

# 21. COLD FLOW RE-EXECUTION

Scenario:

```text
Composable A collects
Composable B collects
↓
cold Flow executes twice
↓
DB/API work duplicated
```

Inspect `stateIn`, `shareIn`, or repository caching behavior before filing a finding.

---

# 22. LIFECYCLE-AWARE COLLECTION

Ask:

> Does a network, sensor, location, or expensive Flow continue executing while the screen is not active?

---

# 23. SIDE EFFECT INVENTORY

Locate repository-wide:

- `LaunchedEffect`
- `DisposableEffect`
- `SideEffect`
- `produceState`
- `rememberCoroutineScope`
- `snapshotFlow`

For each critical effect, document:

```text
Trigger
Key
Work
Cleanup
Ownership
```

---

# 24. `LaunchedEffect`

Ensure the effect key corresponds to the true lifecycle of the underlying task.

---

# 25. MISSING KEY

Example:

```kotlin
LaunchedEffect(Unit) {
    load(id)
}
```

if the same composable instance can remain in composition while `id` changes value.

Verify whether reloading must be triggered.

---

# 26. OVER-BROAD KEY

The inverse problem:

```kotlin
LaunchedEffect(largeObject)
```

where the object reference changes on every composition pass.

This restarts the coroutine/effect far more frequently than intended.

---

# 27. EFFECT RESTART

For each effect, ask:

> What happens to the prior running coroutine when the key changes?

Check cancellation semantics and partial side effects.

---

# 28. STALE CAPTURE IN EFFECT

An effect may capture a callback or value that changes later.

Verify whether `rememberUpdatedState` or another pattern is needed.

Do not insert it blindly.

---

# 29. `rememberUpdatedState`

Look for scenarios where a long-lived effect requires the latest callback without restarting the effect.

Examples:

- timer
- lifecycle observer
- delayed completion callback

---

# 30. `DisposableEffect`

For each, verify symmetry:

```text
register
↓
lifetime
↓
unregister
```

Specifically:

- lifecycle observers
- listeners
- sensors
- callbacks
- media player listeners

---

# 31. CLEANUP

Cleanup must operate on the exact same resource or reference that was registered.

---

# 32. `SideEffect`

Verify whether it is used solely for synchronizing with external non-Compose state and whether the side effect is safe across frequent recompositions.

---

# 33. `produceState`

Verify cancellation handling, key parameters, and error state transitions.

---

# 34. `snapshotFlow`

Check:

- emission frequency
- distinctUntilChanged behavior
- expensive downstream workloads
- cancellation

---

# 35. `rememberCoroutineScope`

Scope belongs to the composition lifetime.

Ensure it is not utilized for work that must outlive the screen or composable.

---

# 36. LONG-RUNNING BUSINESS WORK

If an operation must survive navigation or process/background scenarios, composable scope is not the proper owner.

---

# 37. EVENT HANDLERS

Event handlers must not trigger duplicate side effects due to double-taps or recomposition misunderstandings.

Inspect:

- save
- delete
- purchase
- send
- navigate

---

# 38. DOUBLE TAP

Scenario:

```text
tap
tap
↓
two coroutines
↓
two mutations
```

Check UI guards and server/database idempotency where relevant.

---

# 39. NAVIGATION EFFECTS

Navigation triggered as a side effect of state can cause loops if state remains true following navigation.

Scenario:

```text
success = true
↓
navigate
↓
back
↓
state still true
↓
navigate again
```

---

# 40. ONE-TIME EVENTS

Map the architecture for:

- navigation
- snackbar
- toast
- open dialog

Verify whether an event:

- can be lost
- can be replayed incorrectly
- survives recreation when it should

---

# 41. SHAREDFLOW EVENTS

If `SharedFlow` uses replay > 0, verify that transient events are not replayed to newly attached collectors.

---

# 42. CHANNEL EVENTS

If Channels are used, inspect:

- consumer lifecycle
- lost events
- multiple collectors
- buffering capacity

---

# 43. STATE-BASED EVENTS

If transient events are modeled via persistent state, verify consumption and reset logic.

---

# 44. RECOMPOSITION AUDIT

Do not report every recomposition.

Identify:

- what recomposes
- how often
- how expensive it is
- why

---

# 45. COMPOSITION LOCAL

Review `CompositionLocal` usage.

Look for:

- overly broad mutable state
- hidden implicit dependencies
- frequently changing values at the root of the hierarchy

---

# 46. ROOT STATE CHANGES

If a root-level value modifies a large subtree, assess the recomposition blast radius.

---

# 47. PARAMETERS

For large composables, check whether they receive massive state objects when they only consume a tiny fraction.

However, do not artificially split state objects if Compose stability and conceptual clarity favor the current model.

---

# 48. STABILITY

If a performance finding relies on stability inference, verify compiler reports or tooling rather than guessing.

---

# 49. UNSTABLE COLLECTIONS

Standard List/Map/models may be inferred as unstable depending on type definitions and compiler configuration.

Do not declare this a performance issue without demonstrating a concrete negative recomposition impact.

---

# 50. LAMBDA ALLOCATIONS

Do not report small lambda allocations without evidence.

Compose optimizations and compiler code generation often handle these adequately.

---

# 51. `remember` FOR LAMBDAS

Do not recommend wrapping every lambda function in `remember`.

---

# 52. EXPENSIVE WORK IN COMPOSITION

Look for:

- sorting
- filtering
- JSON parsing
- bitmap creation
- date parsing
- regex compilation
- database access
- file I/O

directly inside the composable body.

---

# 53. SORTING / FILTERING

If a collection contains numerous items and recomposition occurs frequently, calculate the actual cost.

---

# 54. `remember` FOR EXPENSIVE CALCULATION

If an expensive calculation depends on inputs, `remember` keys must cover all variable inputs.

---

# 55. LAZY LISTS

Inspect:

- `LazyColumn`
- `LazyRow`
- grids

Check:

- keys
- contentType
- item state
- nested lists
- scroll state

---

# 56. LAZY LIST KEYS

If items lack keys, check whether insertion or reordering can bind internal state to the wrong item.

---

# 57. INDEX AS KEY

Using index as a key is problematic only when the list order changes or items are inserted/removed dynamically.

Prove the scenario.

---

# 58. MUTABLE ITEM

If an item mutates an internal property without altering its reference or observable identity, check whether the UI receives the update.

---

# 59. NESTED LAZY LISTS

Check:

- scroll conflicts
- measurement passes
- nested scroll state
- rendering performance

---

# 60. SCROLL STATE

If a screen needs to retain its scroll position across navigation or recreation, check ownership and saveability.

---

# 61. `LazyListState`

Ensure `LazyListState` is not recreated unexpectedly due to improper keys or hoisting scopes.

---

# 62. PAGING COMPOSE

If Paging 3 is utilized:

- collection mechanism
- load states
- retry mechanism
- refresh handling
- item keys
- placeholders

---

# 63. LOAD STATE

Differentiate and inspect separately:

- initial load
- refresh
- append
- prepend
- error

Do not trigger full-screen loading indicators for background append operations.

---

# 64. PAGING ERROR

Verify retry flow and ensure users can still view already loaded data while an append fails.

---

# 65. NAVIGATION COMPOSE

Map:

- NavHost
- routes
- arguments
- nested graphs
- deep links
- saved state

---

# 66. ROUTE STRINGS

Look for manual string route concatenation involving raw user input.

Check URL encoding.

---

# 67. NAVIGATION ARGUMENTS

Do not pass large, complex data objects as navigation arguments when an ID or reference can reconstruct state.

Prevent TransactionTooLargeException and stale data bugs.

---

# 68. SAVEDSTATEHANDLE + NAVIGATION

Verify that route parameters become stable inputs inside the ViewModel via SavedStateHandle.

---

# 69. VIEWMODEL SCOPE

Verify that the ViewModel is not scoped too high or too low.

Scenario:

```text
screen A
screen B
↓
shared ViewModel
```

Determine whether shared ViewModel scope is intentional or an accidental bug.

---

# 70. BACK STACK

Check:

- duplicate routes
- pop behavior
- launchSingleTop
- restoreState
- saveState

---

# 71. BOTTOM NAVIGATION

If multiple top-level destinations exist, inspect the back-stack management and state preservation model.

---

# 72. MULTIPLE BACK STACKS

Verify that switching tabs does not lose navigation history or duplicate back stack entries.

---

# 73. NAVIGATION RACE

Rapid double taps can dispatch duplicate navigation commands, pushing identical destinations onto the back stack.

---

# 74. NAVIGATION AFTER PROCESS DEATH

Verify deep links and restored back stacks combined with ViewModel state restoration.

---

# 75. DIALOG COMPOSABLES

Inspect:

- state ownership
- dismissal handling
- rotation behavior
- back press interception
- focus management

---

# 76. DIALOG + ASYNC

Scenario:

```text
open confirm dialog
↓
tap confirm
↓
request starts
↓
dialog dismissed
↓
request fails
```

Ask:

> Where does the user see the failure and how can they retry?

---

# 77. BOTTOM SHEETS

Inspect:

- sheet state management
- coroutine animation transitions
- navigation integration
- configuration recreation
- hidden vs expanded race conditions

---

# 78. MODAL STATE MACHINE

A complex sheet or dialog often involves multiple intermediate states.

Check for invalid state transitions.

---

# 79. ANIMATION

Review:

- `AnimatedVisibility`
- `animate*AsState`
- transitions
- infinite transitions

Do not report an animation as a performance issue without empirical proof.

---

# 80. ANIMATION STATE

If an animation completion callback triggers a business event, check cancellation and restart behavior.

---

# 81. INFINITE TRANSITION

Verify that infinite animations do not remain active when UI is not visible, preventing needless battery and CPU consumption.

---

# 82. REDUCED MOTION / ACCESSIBILITY

If the app features substantial animations, evaluate impact on users with reduced motion preferences.

---

# 83. POINTER INPUT

If custom gestures are implemented:

- `pointerInput`
- `detectTapGestures`
- drag gestures
- transform gestures

verify keys and coroutine cancellation.

---

# 84. `pointerInput` KEY

An improper key can leave stale callbacks or stale state trapped inside gesture coroutines.

---

# 85. CLICKABLE

Verify that custom gesture modifiers have not replaced `Modifier.clickable`, losing:

- accessibility semantics
- keyboard focus and navigation
- ripple and visual interaction feedback
- assistive service integration

without valid justification.

---

# 86. COMBINED CLICKABLE

If long-press actions are supported, verify discoverability and accessibility alternatives.

---

# 87. SEMANTICS

Review custom composables for:

- content description
- role
- selected
- state description
- heading
- mergeDescendants semantics

---

# 88. `clearAndSetSemantics`

Can inadvertently conceal child semantics from accessibility services.

Verify the specific use case.

---

# 89. DECORATIVE CONTENT

Do not assign content descriptions to purely decorative icons or illustrations.

This introduces unnecessary noise for screen readers.

---

# 90. ICON BUTTON

Verify accessible labels for icon-only action elements.

---

# 91. TALKBACK FOCUS ORDER

If a custom layout visually alters element order, check accessibility traversal order.

---

# 92. CUSTOM COMPONENTS

For custom switches, sliders, tabs, or controls, verify that semantics accurately represent their runtime behavior.

---

# 93. FORMS

Analyze Compose forms through:

```text
TextField
↓
local/form state
↓
validation
↓
ViewModel
↓
submit
↓
result
```

---

# 94. TEXTFIELD STATE

Verify single source of truth.

Look for:

```text
server/form value
↓
copied into remember
```

without proper reset or synchronization logic.

---

# 95. ASYNC INITIAL FORM DATA

Scenario:

```text
Composable mounts
↓
empty form state created
↓
network returns entity
↓
new entity data arrives
```

Does the form actually receive the loaded entity, or does `remember` retain the initial empty state?

---

# 96. USER INPUT OVERWRITE

The inverse race condition:

```text
user starts typing
↓
background refresh arrives
↓
effect copies server state into form
↓
user input disappears
```

Identify such race conditions.

---

# 97. VALIDATION

Distinguish between:

- per-keystroke validation
- submit-time validation
- server-side validation

Avoid expensive validation work on every single character change without justification.

---

# 98. FOCUS

Inspect `FocusRequester`, `focusable`, and focus traversal.

Look for:

- focus requested before the element exists in composition
- stale FocusRequester instances
- focus loops
- focus lost following state changes

---

# 99. KEYBOARD

Inspect:

- IME actions
- keyboardOptions
- keyboardActions
- focus next navigation
- form submission

---

# 100. IME ACTION

`Done`, `Next`, or `Search` must correspond to the actual logical user flow.

---

# 101. KEYBOARD HIDING

Do not consider manual keyboard hiding mandatory everywhere.

Evaluate UX and focus state holistically.

---

# 102. BRING INTO VIEW

On long forms, verify that the focused input remains visible above the software keyboard.

---

# 103. WINDOW INSETS

Review:

- status bar
- navigation bar
- IME
- display cutout

Look for double padding or missing system insets.

---

# 104. EDGE-TO-EDGE

If the app implements edge-to-edge display, ensure content is not obscured underneath system bars.

---

# 105. IME INSETS

Pay specific attention to forms, bottom sheets, and sticky action buttons.

---

# 106. ORIENTATION

Compose does not automatically solve every state challenge upon configuration changes.

Re-verify:

- forms
- dialogs
- scroll positions
- selected tabs
- media players

---

# 107. ADAPTIVE LAYOUT

If the application targets tablets or large screens, inspect:

- Window Size Classes
- pane layouts (list-detail)
- state ownership across adaptive panes

Do not mandate adaptive architecture if large screens are outside project scope.

---

# 108. MULTI-WINDOW

If relevant, verify state and layout behavior under split-screen and dynamic window resizing.

---

# 109. PREVIEW CODE

Compose Preview helper code and fake providers must never leak into production runtime behavior.

---

# 110. PREVIEW-ONLY ASSUMPTIONS

A UI that appears functional in Previews can fail catastrophically under real conditions:

- long text and line wrapping
- null fields
- huge list payloads
- loading states
- network error states

---

# 111. DESIGN SYSTEM

If a Compose design system is present, map:

- colors
- typography
- shapes
- spacing
- component primitives

Look for feature-specific business behavior leaked into generic UI primitives.

---

# 112. THEME

Inspect:

- light mode
- dark mode
- dynamic color
- system bars styling

A functional accessibility contrast failure holds higher priority than an aesthetic visual variation.

---

# 113. HARD-CODED COLORS

Report hard-coded color values only when they:

- break dark theme
- fail contrast requirements
- violate state semantics
- disrupt critical theme consistency

---

# 114. HARD-CODED DIMENSIONS

Do not report every fixed `dp` value.

Look for fixed sizes that break under:

- system font scaling
- small screen widths
- tablet orientations

---

# 115. FONT SCALE

Evaluate under large system font settings:

Look for:

- clipped button labels
- obscured controls
- fixed-height containers that overflow

---

# 116. STRING RESOURCES

Hard-coded user-facing strings inside composables hinder localization.

If the app supports i18n, verify consistency.

---

# 117. PLURALIZATION

When displaying item counts, verify proper pluralization formatting where relevant.

---

# 118. LOCALE

Formatting for:

- dates and times
- numbers
- currency values

should rely on platform locale formatters rather than manual string concatenation.

---

# 119. IMAGE LOADING

If using Coil Compose or equivalent, check:

- model specification
- placeholder handling
- error fallback
- request sizing
- memory and disk caching
- lifecycle integration

---

# 120. LARGE IMAGE

Ensure the app does not load full-resolution images for small thumbnail views without downsampling or resize strategies.

---

# 121. ASYNC IMAGE

Loading and error states must be graceful and informative for critical UI images.

---

# 122. CANVAS

If custom Canvas drawing is used:

- expensive drawing routines
- object allocations inside the draw loop
- accessibility alternatives

where relevant.

---

# 123. CUSTOM DRAWING

Do not allocate objects, paths, or paints inside draw phases if provably expensive.

---

# 124. ANDROID VIEW INTEROP

If `AndroidView` is present, inspect:

- factory lambda
- update lambda
- view lifecycle integration
- resource release
- listener cleanup

---

# 125. VIEW INTEROP LEAK

Custom Views can retain Activity Context or external listeners beyond the composable lifecycle.

---

# 126. COMPOSEVIEW IN VIEW/XML APPS

If Compose is hosted inside Fragments or legacy View systems, check the `ViewCompositionStrategy`.

---

# 127. COMPOSITION DISPOSAL

An improper composition strategy can retain the composition tree after the Fragment View lifecycle is destroyed.

---

# 128. WEBVIEW INTEROP

If WebView is hosted inside Compose, specifically check:

- configuration recreation
- state preservation
- destroy cleanup
- back navigation
- memory leaks

---

# 129. MAP VIEW INTEROP

Apply the same rigor to MapView, video players, and heavy legacy Views.

---

# 130. PLAYER INTEROP

Media players must not be instantiated on every recomposition pass.

Map out ownership and lifecycle control.

---

# 131. RESOURCE OWNERSHIP

For every heavyweight resource, ask:

> Who creates it, and who destroys it?

Examples:

- ExoPlayer / Media3
- CameraController
- Sensor listeners
- WebView
- Map instances
- Bluetooth connections

---

# 132. APPLICATION VS SCREEN RESOURCE

A resource intended to outlive a screen must not be scoped to a local composable.

A resource tied to a screen must not be held in a global singleton indefinitely.

---

# 133. CONTEXT

`LocalContext.current` is easily accessible in composables.

Ensure Activity Context is not captured inside long-lived singletons or asynchronous workers.

---

# 134. CONFIGURATION

Reading `LocalConfiguration.current` causes recomposition whenever configuration changes.

If read high in the composition tree, assess the blast radius.

---

# 135. DENSITY

Custom drawing and measurement must correctly handle screen density conversions.

---

# 136. PERFORMANCE PROFILING

If profiling tooling data is available, leverage relevant metrics.

Never assert:

- recomposition counts
- frame times
- jank rates

without empirical measurements.

---

# 137. COMPOSE COMPILER REPORTS

If the project generates Compose compiler metrics/reports, use them as evidence.

Do not interpret every unstable type as a bug.

---

# 138. LAYOUT INSPECTOR

If runtime tooling is active, use it to substantiate concrete recomposition issues.

---

# 139. MACROBENCHMARK

If the app includes performance-critical screens, check for Macrobenchmark coverage.

The absence of macrobenchmarks is P4 unless an acute, reproducible performance issue exists.

---

# 140. BASELINE PROFILE

If present, verify coverage of critical user journeys.

---

# 141. STARTUP

Root composition must not perform blocking operations:

- disk I/O
- network calls
- heavy data parsing

If present, report as an Android performance finding.

---

# 142. MAIN THREAD

Compose does not alter the fundamental rule: blocking I/O does not belong on the Main thread.

---

# 143. REPOSITORY CALL IN COMPOSABLE

If a composable directly triggers repository or network calls during body execution, this constitutes a serious bug.

Trace how many times the invocation can execute across recompositions.

---

# 144. VIEWMODEL CREATION

Inspect `viewModel()` and `hiltViewModel()` call sites.

An incorrect `NavBackStackEntry` scope can bind the ViewModel to an unexpected lifecycle owner.

---

# 145. KEYED VIEWMODEL

If ViewModel instances must vary by entity ID, verify how keys and owners are configured.

---

# 146. DEPENDENCY INJECTION

When Hilt or Koin integrates with Compose, ensure UI does not use DI as an excuse to blur clear component ownership.

---

# 147. PREVIEW + DI

Previews should not require a live production dependency injection graph just to render a UI component.

Treat this as a maintainability improvement rather than a critical runtime bug.

---

# 148. ERROR BOUNDARIES

Compose does not possess an identical error boundary mechanism to web React.

Verify how exceptions from:

- ViewModels
- Flows
- asynchronous coroutines

are handled. Do not invent non-existent Compose error boundary primitives.

---

# 149. CRASH IN COMPOSITION

If untrusted user data causes an unhandled exception inside the composable body, it crashes the entire screen and process.

Specifically check:

- unsafe casts
- index bounds
- null assertions
- string parsing

---

# 150. NULL / EMPTY STATE

For every screen, verify:

- loading
- empty
- error
- content

Identify UI that shows "no data available" when the underlying request actually failed with an error.

---

# 151. STATE RESTORATION

Check:

- selected tab
- scroll position
- form input
- open dialog

against product requirements following process recreation.

---

# 152. PROCESS DEATH

`rememberSaveable` can restore specific state types.

Critical business data requires a durable source of truth (Room, DataStore, backend).

---

# 153. PERSISTED DRAFT

If users enter substantial data, evaluate whether process death results in an unacceptable loss of input.

---

# 154. BACK HANDLING

If `BackHandler` is used, check:

- enabled state conditions
- nested back handlers
- dialog dismissal
- unsaved data warnings

---

# 155. PREDICTIVE BACK

If target Android versions support predictive back, verify compatibility where the app implements custom back handling.

If not verified:

**PREDICTIVE BACK: NOT VERIFIED**

---

# 156. DEEP LINK + COMPOSE NAVIGATION

Check for invalid or missing arguments and proper authentication flow redirection.

---

# 157. AUTH GATE

A UI navigation guard is not server-side authorization, but the Compose flow must cleanly handle:

- loading session
- authenticated
- unauthenticated

without flashing incorrect screens.

---

# 158. AUTH STATE RACE

Scenario:

```text
app starts
↓
auth unknown
↓
UI assumes logged out
↓
navigates to login
↓
stored session finishes loading
```

Check for visual flicker and navigation race conditions.

---

# 159. LOGOUT STATE

Upon logout, reset relevant Compose, ViewModel, and navigation state.

Look for previous user data retained in the back stack.

---

# 160. BACK STACK AFTER LOGOUT

Scenario:

```text
logout
↓
login screen
↓
Back
```

Does pressing Back navigate to a private screen remaining from the old back stack?

---

# 161. USER SWITCHING

Re-verify ViewModel scoped state and retained back stacks when switching accounts.

---

# 162. MULTIPLE WINDOWS / TASKS

If deep links can launch multiple Activity or task instances, verify state assumptions.

---

# 163. ACCESSIBILITY SEMANTICS TESTS

If Compose UI tests exist, check whether they verify accessibility semantics rather than relying solely on arbitrary test tags.

---

# 164. TESTING

Map:

- ViewModel unit tests
- Compose UI tests
- Screenshot tests
- Navigation tests
- Integration tests

---

# 165. COMPOSE UI TESTS

Inspect:

- semantics selectors
- async work and idling resources
- transient UI handling
- scroll interactions
- dialog interactions
- navigation transitions

---

# 166. TESTTAG

`testTag` is helpful for test targeting, but should not replace proper semantic accessibility attributes.

---

# 167. SCREENSHOT TESTS

A visual snapshot does not prove:

- click behavior
- state correctness
- focus behavior
- lifecycle safety

---

# 168. RECOMPOSITION TESTS

Avoid writing brittle tests that expect an exact number of recompositions unless building a dedicated performance benchmark.

---

# 169. STATE RESTORATION TEST

For critical screens, verify:

```text
input
↓
recreate
↓
verify expected state
```

---

# 170. NAVIGATION TEST

Verify:

- direct route navigation
- Back press
- deep links
- double-tap prevention
- logout back stack clearing

---

# 171. PROCESS DEATH LIMITATION

If your test suite cannot simulate true OS process termination, clearly state what the test actually validates.

---

# 172. PREVIEW IS NOT A TEST

Never cite Preview rendering as evidence of runtime correctness.

---

# 173. R8 / RELEASE

While Compose generally cooperates well with R8, custom reflection, serialization, or navigation models can exhibit release-only defects.

Analyze the real codebase.

---

# 174. DEBUG VS RELEASE

Ensure observed performance or timing problems are not merely debug build overhead.

Do not use debug build recomposition counts as production benchmarks.

---

# 175. LAYOUT INSPECTOR OVERHEAD

The Layout Inspector introduces noticeable runtime overhead; avoid treating inspected debug performance as representative of release builds.

---

# 176. FINDING FORMAT

Every serious finding must include:

```text
ID:
Severity:
Category:
Confidence:
Status:

Screen:
Composable:
ViewModel:
File:
Relevant code:

State owner:
Effect/lifecycle:
Affected Android versions:

Problem:

Evidence:

Composition/State Flow:

Reproduction:

Expected behavior:

Actual behavior:

User impact:

Performance/Data impact:

Root cause:

Recommended remediation:

Regression test:

Verification:

Complexity:
XS / S / M / L / XL
```

If a field is not applicable:

**NOT APPLICABLE**

---

# 177. SEVERITY

Use:

## P1 - HIGH

- critical screen crashes
- serious state or data loss
- major navigation loop
- severe lifecycle or resource leak
- Compose bug that blocks core user journey

## P2 - MEDIUM

- reproducible state, recomposition, or side-effect bug with significant impact
- important UI stale or inconsistent behavior

## P3 - LOW

- edge-case UI or state inconsistency
- localized performance or recomposition problem

## P4 - IMPROVEMENT

- architectural, maintainability, or readability enhancement that is not a bug

Reserve P0 exclusively for critical security vulnerabilities or irreversible data loss incidents.

---

# 178. CONFIDENCE

Use:

```text
HIGH
MEDIUM
LOW
```

HIGH:

directly proven by code logic and runtime flow.

MEDIUM:

strong code evidence, but device-specific confirmation is missing.

LOW:

depends on unverified compiler, library, or platform behavior.

---

# 179. STATUS

Use:

```text
CONFIRMED
LIKELY
THEORETICAL
NOT VERIFIED
```

---

# 180. PERFORMANCE STATUS

For performance findings, append:

```text
MEASURED
CODE-LEVEL RISK
NOT MEASURED
```

Never describe something as "slow" as a confirmed fact without empirical measurements.

---

# 181. FALSE-POSITIVE PREVENTION

Before confirming a P1 or P2 finding, review:

1. composable
2. parent composable
3. ViewModel
4. Flow source
5. effect keys
6. lifecycle owner
7. navigation owner
8. state restoration model
9. library behavior
10. Compose and compiler version
11. existing automated tests

Never conclude from a single isolated line.

---

# 182. DO NOT REFACTOR

During the audit:

- do not move state around
- do not insert `remember`
- do not add memoization
- do not modify navigation
- do not change ViewModel scope
- do not introduce MVI
- do not refactor the design system

Complete the audit first.

---

# 183. OUTPUT - JETPACK_COMPOSE_AUDIT.md

Structure the final output document:

## 1. Executive Summary

- Compose stack
- architecture overview
- top state and lifecycle risks
- performance status
- production readiness

## 2. Compose Architecture Map

## 3. State Ownership Map

## 4. ViewModel / StateFlow Audit

## 5. `remember` / `rememberSaveable` Audit

## 6. Derived State Audit

## 7. Side Effects Audit

## 8. Coroutine Ownership Audit

## 9. Recomposition Audit

## 10. Stability Audit

## 11. Lazy List Audit

## 12. Navigation Compose Audit

## 13. Form State Audit

## 14. Focus / IME / Insets Audit

## 15. Accessibility Semantics Audit

## 16. View Interop Audit

## 17. Resource Lifecycle Audit

## 18. Performance Audit

## 19. State Restoration / Process Death

## 20. Authentication / Logout State

## 21. Testing Audit

## 22. Release Build Risks

## 23. Findings Summary

| ID | Severity | Category | Screen | Problem | Confidence | Status |
|---|---|---|---|---|---|---|

## 24. P1 Findings

## 25. P2 Findings

## 26. P3 Findings

## 27. P4 Improvements

## 28. Things Done Well

## 29. Unknown / Not Verified

## 30. Remediation Roadmap

---

# 184. COMPOSE STATE MATRIX

For critical screens, document:

| Screen | State source | Owner | Saveable | Durable | Risk |
|---|---|---|---|---|---|

---

# 185. EFFECT MATRIX

For important effects:

| Composable | Effect | Key | Work | Cleanup | Risk |
|---|---|---|---|---|---|

---

# 186. RECOMPOSITION MATRIX

For hotspots only:

| Composable | Trigger | Expensive work | Measured | Risk |
|---|---|---|---|---|

Do not invent arbitrary recomposition counts.

---

# 187. NAVIGATION MATRIX

| Destination | ViewModel scope | Arguments | Deep link | Restoration | Risk |
|---|---|---|---|---|---|

---

# 188. STATE RESET PASS

After the initial audit, for each screen ask:

> When should this state reset?

Scenarios:

- navigate away
- logout
- new entity selected
- filter change
- account switch
- retry action

Look for state that outlives its intended lifecycle.

---

# 189. STATE SURVIVAL PASS

Then reverse the question:

> Which state disappears earlier than it should?

Scenarios:

- rotation
- configuration change
- tab switching
- process recreation
- app sent to background

---

# 190. EFFECT RESTART PASS

For each `LaunchedEffect`, ask:

> Which change should restart this task?

Then compare with the actual configured keys.

---

# 191. EFFECT CANCELLATION PASS

Ask:

> What happens if this effect is cancelled halfway through?

Specifically:

- save operation
- animation
- navigation event
- repository transaction

---

# 192. FAST INPUT PASS

Simulate:

```text
type fast
↓
change query
↓
switch filter
↓
navigate
```

Look for stale asynchronous results and improper effect restarts.

---

# 193. DOUBLE TAP PASS

For all critical click actions:

```text
tap twice
```

Check navigation stacking and duplicate mutations.

---

# 194. ROTATION PASS

Re-evaluate:

- forms
- dialogs
- sheets
- tabs
- lazy lists
- media player
- search inputs

---

# 195. PROCESS DEATH PASS

Ask:

> What happens if Android terminates the process right now?

Do not answer "the ViewModel preserves the state."

A ViewModel does not survive process death.

---

# 196. SLOW DEVICE PASS

Assume a low-end CPU and frequent recomposition passes.

Identify only concrete, demonstrable expensive hotspots.

---

# 197. LARGE DATA PASS

Mentally scale up:

- 20 list items to 2,000
- 10 search results to 10,000

Ask where the Compose tree or inline data processing breaks down.

---

# 198. ACCESSIBILITY PASS

Envision a TalkBack user.

For each custom component, ask:

- what does the user hear
- how do they activate it
- how do they perceive current state

---

# 199. DARK MODE / FONT SCALE PASS

Inspect critical UI under:

- dark theme
- largest system font scale

Look for clipping or unreadable contrast.

---

# 200. LOGOUT PASS

Scenario:

```text
private screen
↓
logout
↓
login screen
↓
Back
```

Check:

- navigation back stack
- ViewModel state
- remembered UI state
- cached private content

---

# 201. SECOND PASS

After the state and lifecycle passes, re-walk every finding as its own skeptic:

- trace the actual recomposition, effect or state path in code, or reproduce it on a device or emulator; do not rely on the pattern name alone
- check whether a key, `remember` scope, `rememberSaveable`, `derivedStateOf`, stable types, the ViewModel or the navigation back stack already prevents the failure
- confirm the Compose, Kotlin and AGP versions the finding depends on; compiler and runtime behavior differ between versions
- look for hidden paths: the same composable reused in lists, dialogs, bottom sheets, previews, other navigation destinations or configuration variants
- check timing and scale: fast repeated input, rotation during an effect, process death during a pending operation, large lists, slow devices
- for performance findings, require evidence (recomposition counts, Layout Inspector, traces, benchmarks), not only suspicion

A finding without a concrete trigger and observable impact is downgraded to THEORETICAL or NOT VERIFIED.

---

# 202. FINAL QUALITY GATE

Before submitting the final report, verify:

- you did not report recomposition simply because it occurs
- you did not blindly recommend `remember`
- you did not suggest `derivedStateOf` as a universal panacea
- effect findings identify a concrete key or lifecycle flaw
- stale state findings have a provable source-of-truth breakdown
- ViewModel scopes were validated against actual NavBackStackEntry owners
- process death was not confused with simple configuration change
- transient events were not unthinkingly routed to SharedFlow without analysis
- performance claims are not fabricated
- accessibility analysis is not reduced to just checking `contentDescription`
- custom gesture modifiers verify semantic accessibility
- lazy list key issues present a real reorder or insertion hazard
- release build behavior is distinguished from debug overhead
- bugs and P4 improvements are strictly separated
- remediation addresses the root cause, not merely the symptom

---

# FINAL RULE

I do not want a generic report stating:

> Use remember, derivedStateOf, stable models, and reduce recompositions.

That is not a Compose audit.

I am looking for concrete issues such as:

```text
Screen displays item A
↓
remember { mutableStateOf(item.name) }
↓
navigation switches item to B without remounting
↓
remember lacks item.id key
↓
UI continues displaying name of item A
```

or:

```text
LaunchedEffect(Unit)
↓
loads data for userId 1
↓
same composable receives userId 2
↓
effect does not restart
↓
screen remains stuck with stale data
```

or:

```text
LaunchedEffect(searchQuery)
↓
request A starts
↓
query changes
↓
A is cancelled
↓
repository call ignores cancellation and commits result anyway
↓
newer state is overwritten by outdated result
```

or:

```text
success state = true
↓
Composable navigates
↓
user presses Back
↓
ViewModel still holds success = true
↓
screen immediately navigates again in a loop
```

or:

```text
form value from ViewModel
↓
copied into remember local state
↓
background refresh changes server value
↓
local state remains stale
↓
UI displays data that is no longer the source of truth
```

or:

```text
Player created inside composable body
↓
recomposition occurs
↓
new Player instance created
↓
old instance is never released
↓
audio/resource leak occurs
```

Think through:

- composition lifetime
- state ownership
- effect keys
- cancellation
- recomposition
- navigation back stack
- configuration change
- process death
- resource ownership
- asynchronous ordering

If evidence is insufficient:

**NOT VERIFIED.**

If the issue is merely a potential optimization:

**P4 - IMPROVEMENT.**

If performance is suspicious based purely on code analysis:

**CODE-LEVEL RISK.**

It is far better to identify 8 genuine Compose state/effect defects than to produce 80 generic recommendations.

The goal is a forensically precise Jetpack Compose audit from which every serious finding can directly translate into:

- reproduction steps
- fix
- regression test
- lifecycle verification
- production validation

分类与标签

编程开发、安卓开发、状态管理、生命周期、界面性能、无障碍审查

前往 AI 图片创作