返回提示词目录第 1343 页

安卓生命周期缺陷排查与验证

沿旋转、导航和进程终止场景追踪组件与异步任务,输出状态丢失、泄漏及崩溃的复现与修复依据。

完整提示词

I want you to perform a maximally deep, systematic, and evidence-first analysis of Android lifecycle issues across the entire application.

Primary objective:

> Identify genuine bugs that arise when an Activity, Fragment, View, Compose screen, ViewModel, Service, coroutine, observer, or other resource lives shorter or longer than the implementation assumes.

This is not:

- a generic Android code review
- an architecture audit of the entire application
- a generic list of lifecycle best practices
- an automated refactoring task
- advice to blindly move everything into a ViewModel
- a superficial search for every `onCreate` or `onDestroy`
- an attempt to save every piece of state at all costs

The focus is on bugs that emerge due to:

- configuration changes
- screen rotation
- Fragment View lifecycle disconnects
- navigation events
- Activity recreation
- app background/foreground transitions
- process death
- system resource reclamation
- observer lifecycle mismatches
- coroutine ownership flaws
- unreleased resources and leaks
- service lifecycle mismanagement
- delayed callbacks
- asynchronous results arriving too late
- faulty state restoration

Priority:

**correctness > data preservation > lifecycle safety > resource safety > UX continuity > architecture elegance**

It is far better to find 8 genuine lifecycle bugs than to write 80 generic Android recommendations.

---

# 1. DETERMINE ANDROID STACK

Prior to analysis, identify:

- Android Gradle Plugin
- Kotlin version
- `minSdk`
- `targetSdk`
- Activities
- Fragments
- Jetpack Compose
- Navigation Component
- ViewModel
- SavedStateHandle
- LiveData
- Flow / StateFlow / SharedFlow
- Coroutines
- Room
- WorkManager
- Services
- Media components
- Dependency injection framework
- Testing framework

Specifically determine whether the application uses:

- single Activity architecture
- multiple Activities
- Fragment-based navigation
- Compose Navigation
- hybrid Views + Compose
- custom lifecycle ownership mechanisms

Do not rely on outdated lifecycle assumptions.

---

# 2. MAP LIFECYCLE ARCHITECTURE

Before filing findings, map the primary lifecycle owners.

Example:

```text
Application
↓
Activity
↓
Fragment
↓
Fragment View
↓
ComposeView / Composable
↓
ViewModel
↓
Coroutine / Flow collector
↓
Resource
```

For each critical component of the application, determine:

- what instantiates it
- when it becomes active
- who owns its lifecycle
- when it must terminate
- what state must survive
- what state must not survive

---

# 3. CORE PRINCIPLE

For every state, coroutine, listener, or resource, ask two fundamental questions:

> Does it live shorter than it should?

and:

> Does it live longer than it should?

The first group produces:

- lost state
- interrupted background operations
- accidental UI resets

The second group produces:

- memory leaks
- stale callbacks
- duplicate event execution
- invalid UI access crashes
- cross-screen state bleeding

---

# 4. ACTIVITY LIFECYCLE

For each critical Activity, examine:

- `onCreate`
- `onStart`
- `onResume`
- `onPause`
- `onStop`
- `onDestroy`

Look for code that erroneously assumes:

```text
onDestroy = application exit
```

This is not a reliable platform guarantee.

---

# 5. ACTIVITY RECREATION

Mentally trace:

```text
Activity A
↓
configuration change
↓
old Activity destroyed
↓
new Activity created
```

Check:

- state persistence
- active observers
- pending callbacks
- open dialogs
- running coroutines
- bound adapters
- allocated system resources

---

# 6. CONFIGURATION CHANGE

Evaluate at minimum:

- screen rotation
- device locale change
- font scale adjustments
- dark / light mode toggle
- screen size change (multi-window / folding display)

Not every configuration variation is relevant to every project, but core variations must be evaluated.

---

# 7. `configChanges`

If the manifest specifies:

```xml
android:configChanges
```

determine why.

Do not treat it automatically as an antipattern.

However, verify whether it merely masks an unhandled underlying lifecycle defect that the application fails to resolve properly.

---

# 8. STATE AFTER ROTATION

For critical screens, check:

- selected tab
- form input data
- selected item
- applied filters
- scroll position
- active dialog
- media playback position
- search query
- loading indicator state

Classify each:

```text
SHOULD SURVIVE
SHOULD RESET
NOT VERIFIED
```

---

# 9. VIEWMODEL

A ViewModel survives configuration changes, but it does NOT survive process death.

Never rely on the assertion:

> State is safe because it is stored in the ViewModel.

without evaluating OS process death.

---

# 10. VIEWMODEL SCOPE

Determine the true ViewModelStoreOwner:

- Activity
- Fragment
- parent Fragment
- navigation graph back stack entry
- Compose destination

An improper scope can produce:

- state that resets prematurely
- state that lingers indefinitely
- state unintentionally shared with the wrong screen

---

# 11. ACTIVITY-SCOPED VIEWMODEL

If two Fragments share an Activity-scoped ViewModel, verify whether state sharing is intentional.

Scenario:

```text
Fragment A
↓
sets selectedItem
↓
Fragment B
↓
unexpectedly inherits same state
```

---

# 12. NAVIGATION GRAPH VIEWMODEL

If a ViewModel must live across an entire nested user journey, verify that it is not scoped to a single leaf destination.

---

# 13. STALE VIEWMODEL

Scenario:

```text
screen item A
↓
ViewModel created
↓
navigate to item B
↓
same ViewModel retained
↓
state for A remains
```

Check keys, owners, and argument handling.

---

# 14. FRAGMENT LIFECYCLE

Strictly distinguish:

```text
Fragment lifecycle
```

from:

```text
Fragment View lifecycle
```

This divergence is among the most prolific sources of Android bugs and memory leaks.

---

# 15. FRAGMENT VIEW DESTRUCTION

Scenario:

```text
Fragment remains on back stack
↓
onDestroyView()
↓
Fragment object still alive
```

Any lingering reference to the destroyed View hierarchy becomes a memory leak or crash hazard.

---

# 16. VIEW BINDING

Inspect the pattern:

```kotlin
private var _binding: FragmentXBinding? = null
```

Ensure the binding is cleared inside:

```text
onDestroyView
```

Do not file an issue if the codebase uses a lifecycle-safe auto-clearing delegate property.

---

# 17. STALE VIEW REFERENCE

Search for:

- cached View instances
- adapter references
- dialog binding references
- RecyclerView references
- Toolbar references
- TextView / View references

that outlive `onDestroyView`.

---

# 18. `viewLifecycleOwner`

For UI observation within a Fragment, verify the usage of:

```text
viewLifecycleOwner
```

instead of the Fragment instance itself whenever the observer interacts directly with the View hierarchy.

---

# 19. OBSERVER AFTER DESTROYVIEW

Scenario:

```text
Fragment View destroyed
↓
Flow / LiveData emits
↓
observer tied to Fragment lifecycle
↓
callback attempts to update old binding
```

Consequences include:

- NullPointerException / crash
- silent stale UI updates
- View hierarchy leaks

---

# 20. COMPOSE IN FRAGMENT

When `ComposeView` is hosted within a Fragment View lifecycle, verify the `ViewCompositionStrategy`.

An improper disposal strategy leaves the composition active after the Fragment View is destroyed.

---

# 21. COMPOSE LIFECYCLE

If the application is Compose-first, map:

- composition lifetime
- NavBackStackEntry lifetime
- Activity lifecycle
- ViewModel lifecycle

Do not conflate a composable leaving the composition tree with system process death.

---

# 22. `LaunchedEffect`

When a composable leaves the composition tree, active `LaunchedEffect` jobs are cancelled.

Ask:

> Is cancellation desirable for the underlying operation?

If an operation must survive the screen, the composable scope is the wrong owner.

---

# 23. `rememberCoroutineScope`

Applies the same rule.

The scope is bounded by the composable lifetime in the composition.

Do not use it for durable business operations that must complete across navigation transitions.

---

# 24. LIFECYCLE COROUTINES

Map:

- `lifecycleScope`
- `viewLifecycleOwner.lifecycleScope`
- `viewModelScope`
- custom CoroutineScope

For every launched job, establish its intended lifetime.

---

# 25. WRONG SCOPE

Example:

```text
database sync
↓
launched in Fragment viewLifecycleOwner scope
↓
user navigates away
↓
sync cancelled prematurely
```

If the sync must complete regardless of UI presence, the scope is misplaced.

---

# 26. INVERSE SCOPE HAZARD

Example:

```text
UI animation callback
↓
launched in application scope
↓
screen destroyed
↓
callback retains reference / state
```

The job outlives its consumer, leaking memory or executing useless work.

---

# 27. CUSTOM COROUTINE SCOPE

For every custom CoroutineScope, determine:

- owner instance
- associated Job
- CoroutineDispatcher
- cancellation point

If cancellation is never invoked, evaluate leak and orphan execution risks.

---

# 28. GLOBALSCOPE

Inspect all usages of `GlobalScope`.

A finding requires demonstrating a concrete ownership or cancellation failure, rather than merely pointing out that `GlobalScope` is deprecated or discouraged.

---

# 29. ASYNC CALLBACK AFTER SCREEN DETACHMENT

A prevalent failure pattern:

```text
Screen A
↓
request starts
↓
user navigates away
↓
response arrives
↓
callback updates Screen A
```

Verify whether:

- the callback retains a valid lifecycle owner
- the result should be ignored safely
- the result belongs to shared application state
- a navigation action is mistakenly triggered on an inactive screen

---

# 30. DELAYED CALLBACK

Look for:

- `Handler`
- `postDelayed`
- timer / TimerTask
- coroutine `delay()`
- work scheduler

that accesses an Activity, Fragment, or View after a delay.

---

# 31. HANDLER

Verify callback removal (`removeCallbacksAndMessages`) where required.

Especially when Runnables capture the enclosing Activity or View.

---

# 32. TIMER

If a timer belongs to a specific screen, verify when and where it is cancelled.

---

# 33. COUNTDOWN

If a countdown timer must continue across background transitions or screen recreation, do not base it on memory-only interval decrements.

Calculate remaining duration from a canonical absolute timestamp.

Report only if the current implementation demonstrably drifts or resets.

---

# 34. PROCESS DEATH

Treat OS process death as an entirely distinct scenario from configuration rotation.

Simulate:

```text
app backgrounded
↓
Android OS terminates process to reclaim memory
↓
user returns to app via Recents screen
```

Determine what state successfully reconstructs.

---

# 35. VIEWMODEL AFTER PROCESS DEATH

The previous ViewModel instance is destroyed.

The new ViewModel instance must receive sufficient state to reconstruct the screen.

---

# 36. NAVIGATION ARGUMENTS

Ensure that critical destinations can be reconstructed from:

- route arguments
- persistent entity IDs
- durable storage sources

rather than relying exclusively on in-memory state passed from a previous screen.

---

# 37. SCREEN DEPENDS ON PREVIOUS SCREEN

Problematic flow:

```text
List screen
↓
stores selected object globally in a singleton
↓
Detail screen reads global object
```

If process death occurs while Detail is open:

```text
global object is null
```

Detail screen lacks necessary data for reconstruction, leading to a crash or empty screen.

---

# 38. PASS ID, NOT WHOLE EPHEMERAL STATE

Passing an entity ID and querying durable storage is generally more resilient than passing large ephemeral state objects.

However, do not mandate architectural rewrites if the existing approach safely persists state.

---

# 39. SAVEDSTATEHANDLE

Inspect:

- which values are persisted
- payload sizes
- serialization integrity
- default fallback values
- restoration logic

---

# 40. STATE RESTORATION SOURCE

For each critical screen, designate its restoration source:

```text
ViewModel only
SavedStateHandle
Room / SQLite database
DataStore
navigation argument
local file
remote server
none
```

---

# 41. DURABLE VS EPHEMERAL

Differentiate:

**Ephemeral UI state**

- active tab index
- list scroll position
- temporary UI selection

**Durable user data**

- form draft
- newly created record
- in-progress payment
- media recording
- downloaded file

Durable user data must never rely solely on volatile lifecycle state.

---

# 42. FORM PROCESS DEATH

For complex or multi-step forms, test:

```text
user enters 20 fields
↓
app sent to background
↓
process death occurs
↓
user returns to app
```

Identify whether product requirements dictate:

- restoring all entered values
- restoring saved draft
- resetting the form

Document actual vs expected behavior.

---

# 43. UNSAVED CHANGES

If a screen contains unsaved user input, check:

- system Back press
- forward navigation
- configuration change
- process termination

While not every app requires full autosave, state behavior must be consistent and predictable.

---

# 44. ACTIVITY RESULT API

Review:

- file pickers
- runtime permissions
- camera capture
- external Activity integrations

Ensure lifecycle-safe registration before `STARTED` lifecycle state.

---

# 45. LEGACY `startActivityForResult`

If legacy result methods are present, verify that the implementation handles recreation safely.

Do not report an issue solely because a newer API exists.

---

# 46. RESULT AFTER RECREATION

Scenario:

```text
external picker opened
↓
Activity recreated while picker is in foreground
↓
result returns to recreated Activity
```

Does the result callback still reach the proper consumer?

---

# 47. FRAGMENT RESULT API

If the Fragment Result API is used, check lifecycle owners and result key collisions.

---

# 48. DIALOGFRAGMENT

Inspect:

- state restoration across rotation
- callback owner attachment
- duplicate dialog displays
- FragmentTransaction state loss (`IllegalStateException`)

---

# 49. CUSTOM DIALOG

If standard Dialog instances are manually attached to an Activity or Fragment, check for window leaks and recreation crashes.

---

# 50. WINDOW LEAK

Scenario:

```text
Activity finishing
↓
dialog still attached to WindowManager
```

Identify concrete `WindowLeaked` hazards.

---

# 51. TOAST

Standard system Toasts rarely cause lifecycle issues, but custom Toast Views holding an Activity context can cause leaks.

Review only if custom implementations exist.

---

# 52. SNACKBAR

If a Snackbar references a destroyed View after navigation, it may crash or display on an unexpected screen.

---

# 53. LIVE DATA

For each LiveData observer, inspect the associated LifecycleOwner.

Specifically:

- Activity
- Fragment
- Fragment View

---

# 54. `observeForever`

Carefully analyze every invocation of `observeForever`.

Ask:

> Who removes this observer and when?

If never removed, this constitutes a confirmed leak and duplicate invocation hazard.

---

# 55. FLOW COLLECTION

Verify that Flow collection aligns with the UI lifecycle.

---

# 56. `repeatOnLifecycle`

If used, verify the target lifecycle state:

- STARTED
- RESUMED

and confirm it suits the workload.

---

# 57. FLOW RESTART

`repeatOnLifecycle` restarts its coroutine block each time the lifecycle re-enters the target state.

Ask:

> Is this workload safe to restart repeatedly?

---

# 58. DUPLICATE API REQUEST

Scenario:

```text
STARTED
↓
collector starts
↓
STOPPED
↓
cancelled
↓
STARTED
↓
collector starts again
↓
cold Flow repeats expensive API request
```

If repeated execution is unintended, this is an acute lifecycle defect.

---

# 59. HOT FLOW

If an upstream producer operates independently of active collectors, inspect its sharing scope.

---

# 60. `stateIn`

The coroutine scope determines how long the upstream Flow remains active.

An overly broad scope wastes resources.

An overly narrow scope restarts expensive operations unnecessarily.

---

# 61. `SharingStarted`

Evaluate `WhileSubscribed`, `Eagerly`, and `Lazily` in relation to the specific feature requirements.

---

# 62. SENSOR LISTENERS

If the app utilizes:

- accelerometer
- gyroscope
- proximity sensor
- location services

verify registration and unregistration symmetry across lifecycle callbacks.

---

# 63. LOCATION

Ask:

> Are location updates necessary while the Activity is not visible?

If not, listeners must cease updates in appropriate lifecycle transitions.

---

# 64. CAMERA

The camera hardware lifecycle must adhere to its lifecycle owner.

Check:

- backgrounding
- orientation changes
- permission revocation
- process recreation

---

# 65. BLUETOOTH

If active connections or GATT sessions exist, determine whether ownership belongs to:

- a specific screen
- an Activity
- a foreground Service
- an application-level session manager

An incorrect owner drops connections prematurely or keeps hardware active indefinitely.

---

# 66. USB

Apply the same criteria to USB and external hardware peripheral connections.

---

# 67. MEDIA PLAYER

Map:

```text
create
↓
prepare
↓
play
↓
pause / background
↓
release
```

Ask:

> Must playback survive when the user navigates away from the screen?

---

# 68. PLAYER IN ACTIVITY

If background audio is required, scoping the player to the Activity lifecycle is too restrictive.

---

# 69. PLAYER IN GLOBAL SINGLETON

If playback is strictly confined to a specific feature, retaining the player in a global singleton causes unnecessary resource retention.

---

# 70. PLAYER LISTENERS

Ensure player listeners holding references to screens or Views are unregistered upon destroy events.

---

# 71. WEBVIEW

WebViews are heavy resources holding Window and Context references.

Inspect:

- ownership
- navigation back stack behavior
- cleanup routines
- explicit `destroy()` calls
- Activity Context leaks

---

# 72. MAP VIEW

Map SDK components require explicit lifecycle forwarding.

Verify that `onCreate`, `onStart`, `onResume`, `onPause`, `onStop`, `onDestroy`, `onSaveInstanceState`, and `onLowMemory` are properly forwarded if mandated by the SDK.

---

# 73. AD SDK

Third-party ad network callbacks can fire after the parent Activity has been destroyed.

Verify that callbacks validate the current screen and lifecycle state before mutating UI.

---

# 74. BILLING

Google Play Billing purchase flows can span across Activity recreation.

Ensure purchase fulfillment does not depend exclusively on the volatile Activity instance that initiated the purchase.

---

# 75. PAYMENT EXTERNAL ACTIVITY

Apply the same rigor to third-party payment gateways and external authentication Activities.

---

# 76. NOTIFICATION FLOW

Tapping a notification can launch a new Activity instance while an existing one is already running.

Check:

- launch modes
- intent flags (`FLAG_ACTIVITY_CLEAR_TOP`, `FLAG_ACTIVITY_SINGLE_TOP`)
- task back stack composition

---

# 77. MULTIPLE ACTIVITY INSTANCES

Scenario:

```text
existing MainActivity
↓
notification clicked
↓
new duplicate MainActivity created
```

If the application assumes a singleton instance, state divergence occurs.

---

# 78. LAUNCH MODE

Inspect:

- standard
- singleTop
- singleTask
- singleInstance

Do not modify launch modes without understanding the complete back-stack implications.

---

# 79. `onNewIntent`

When using `singleTop` or `singleTask`, verify that `onNewIntent` updates the active UI state and sets the new Intent.

---

# 80. DEEP LINK TO EXISTING ACTIVITY

An existing ViewModel or state may remain bound to previous arguments if `onNewIntent` fails to refresh inputs.

---

# 81. BACKGROUND / FOREGROUND

Simulate:

```text
app active
↓
Home button pressed
↓
app idle in background for 5 minutes
↓
app brought back to foreground
```

Check:

- session authentication validity
- stale data refreshing
- active timers
- paused hardware resources

---

# 82. LONG BACKGROUND

Repeat the simulation with:

- several hours in background
- expired auth tokens
- backend data modifications

Do not invent arbitrary timeout thresholds not specified by product requirements.

---

# 83. SESSION EXPIRY

If an authentication session expires while the app is in the background:

```text
foreground resume
```

must cleanly transition to an unauthenticated state without flashing protected screens.

---

# 84. FOREGROUND REFRESH

If data is refreshed on every `onResume`, check for:

- duplicate network requests
- redundant reloads when returning from sub-dialogs
- unnecessary reloads when returning from runtime permission prompts

`onResume` executes far more often than initial app startup.

---

# 85. `onResume` AS APP START

Look for the false assumption:

```text
onResume = user launched app from home screen
```

This is incorrect.

---

# 86. `onPause` AS APP BACKGROUND

Look for the inverse false assumption:

A dialog or external Activity can trigger `onPause` without the application being sent to the background.

---

# 87. PROCESS LIFECYCLE OWNER

If the application tracks global foreground / background transitions via `ProcessLifecycleOwner`, verify that the behavior matches business intent.

---

# 88. GLOBAL APP LOCK

If the app requires PIN or biometric re-authentication after being backgrounded, verify:

- `ProcessLifecycleOwner` integration
- external Activity exclusions (camera, file picker)
- configuration change resilience

Ensure the lock screen is not triggered during screen rotation or system dialogs.

---

# 89. TIMER FOR BACKGROUND

If measuring elapsed time since the app entered the background, use system wall-clock timestamps rather than counting lifecycle callbacks.

---

# 90. APP STARTUP

`Application.onCreate()` can execute multiple times if the application defines components running in separate processes.

Check only if multi-process components exist.

---

# 91. MULTI-PROCESS

If the manifest specifies:

```text
android:process
```

map which initialization code runs within each distinct process.

---

# 92. STATIC SINGLETON

A static singleton is per-process, not globally shared across separate OS processes.

---

# 93. SERVICES

For every Service, classify:

- started service
- bound service
- foreground service
- lifecycle owner

---

# 94. STARTED SERVICE

Verify what occurs if the OS terminates the process or service.

---

# 95. `START_STICKY`

If used, verify behavior when the service is restarted with a null Intent.

---

# 96. `START_REDELIVER_INTENT`

Check for duplicate processing scenarios upon restart.

---

# 97. BOUND SERVICE

Map the bind / unbind lifecycle.

Look for:

- connection leaks
- callbacks executing after unbind
- leaked Activity references

---

# 98. FOREGROUND SERVICE

Check:

- notification posting requirement
- `stopForeground` / `stopSelf` invocation
- completion handling
- process recreation

---

# 99. WORKMANAGER

Worker execution lifecycle is decoupled from the UI lifecycle.

Do not dispatch durable background work via Activity coroutines if it must survive app termination.

---

# 100. UI + WORKER DUPLICATION

Scenario:

```text
foreground sync starts in UI coroutine
↓
app sent to background
↓
periodic or constraint Worker starts identical sync
```

Verify deduplication and idempotency safeguards.

---

# 101. BROADCAST RECEIVER

Any dynamically registered `BroadcastReceiver` must have an explicit unregistration owner.

---

# 102. REGISTER/UNREGISTER SYMMETRY

Verify matching pairs:

```text
register -> unregister
bind -> unbind
addListener -> removeListener
start -> stop
open -> close
```

Ensure symmetry for every registered resource.

---

# 103. OBSERVER DUPLICATION

Scenario:

```text
onStart
↓
register observer
↓
onStop
↓
observer not removed
↓
onStart
↓
register observer again
```

A single emission will trigger duplicate callbacks.

---

# 104. EVENT BUS

If an event bus is used, verify subscription and unsubscription lifecycles.

---

# 105. RXJAVA

If the project uses RxJava:

- `CompositeDisposable` management
- disposal points
- repeated subscriptions
- stream completion

---

# 106. DISPOSABLE OWNER

Ask:

> When does this subscription cease to make sense?

Disposal must align with that exact lifecycle boundary.

---

# 107. CALLBACK API

Legacy SDK callbacks are frequently lifecycle-unaware.

Map registration and cancellation handling.

---

# 108. CALLBACK TO COROUTINE BRIDGE

When using `suspendCancellableCoroutine`, check:

- `invokeOnCancellation` cleanup
- listener unregistration
- double-resume prevention

---

# 109. DOUBLE RESUME

A callback API that can invoke both success and error, or invokes a callback after cancellation, will trigger an `IllegalStateException: Already resumed`.

---

# 110. RESOURCE CLOSURE

Inspect:

- `Cursor`
- `InputStream`
- `OutputStream`
- raw file descriptors
- `ParcelFileDescriptor`

Verify explicit closure via `use {}` or `close()`.

---

# 111. DATABASE CURSOR

If raw SQLite or Cursors are used, verify their lifecycle management.

---

# 112. ACTIVITY CONTEXT

Look for Activity Context instances retained in:

- singletons
- repositories
- companion objects
- long-lived asynchronous callbacks

---

# 113. FRAGMENT REFERENCE

ViewModels and repositories must never hold Fragment references.

If found, flag as a critical leak and architectural defect.

---

# 114. VIEW REFERENCE IN VIEWMODEL

This is a high-confidence signal.

Verify the specific use case.

A ViewModel must not hold references to Android View hierarchy objects.

---

# 115. NAVCONTROLLER REFERENCE

Long-lived architectural layers must not retain references to `NavController`.

Navigation events should be dispatched through a lifecycle-safe event stream.

---

# 116. ADAPTER CONTEXT

A RecyclerView adapter retaining an Activity Context is not automatically a leak if the adapter shares the Activity lifetime.

A leak occurs only when the adapter or its pool outlives the Activity.

---

# 117. LISTENER CAPTURE

Anonymous inner classes and lambdas can implicitly capture the enclosing Activity or Fragment.

Trace where the listener reference is stored.

---

# 118. STATIC CALLBACK

Static or global callback registries retaining Activity references represent high-risk memory leaks.

---

# 119. DELAYED NAVIGATION

Scenario:

```text
delay(2000)
↓
navigate()
```

If the user navigates away before the delay expires, check whether the target destination or current destination is validated prior to navigating.

---

# 120. SPLASH SCREEN

If a splash screen relies on a fixed time delay, check:

- screen rotation
- duplicate navigation events
- background transitions
- process recreation

---

# 121. AUTH SPLASH

Authentication routing must be driven by authentication state, not arbitrary lifecycle delay timings.

---

# 122. INITIALIZATION RACE

Scenario:

```text
Activity starts
↓
session loading asynchronously
↓
navigation routing runs prematurely
↓
navigates to unauthenticated screen mistakenly
```

---

# 123. PERMISSION DIALOG

Displaying a system permission dialog triggers lifecycle state changes (`onPause`, `onStop`).

Ensure `onResume` side effects do not interpret the return from a permission dialog as a brand-new app launch.

---

# 124. SETTINGS SCREEN RETURN

When a user navigates to Android System Settings to grant a permission:

```text
app sent to background
↓
user changes setting
↓
app returns to foreground
```

The feature must re-evaluate the actual permission grant status on return.

---

# 125. CAMERA / EXTERNAL APP RETURN

Apply the same verification to external camera, gallery, or document picker returns.

---

# 126. BACK PRESS

A Back press can:

- pop a Fragment
- finish an Activity
- dismiss a Dialog
- exit a nested navigation graph

Verify critical state persistence across each outcome.

---

# 127. CUSTOM BACK HANDLER

Custom back handling must not bypass platform navigation or state saving unnecessarily.

---

# 128. UNSAVED FORM + BACK

If a confirmation dialog appears on Back:

```text
Back pressed
↓
dialog shown
↓
screen rotated
```

Verify that both the dialog and underlying form data remain intact.

---

# 129. DOUBLE BACK

Rapid repeated Back presses can cause duplicate pop actions or app exit crashes in flawed implementations.

---

# 130. ACTIVITY FINISH

If an asynchronous callback completes after `Activity.finish()` has been called, ensure it does not attempt UI manipulations.

---

# 131. `isFinishing`

Do not rely on `isFinishing` as a universal lifecycle safeguard.

An Activity can be destroyed by the system due to memory reclamation or configuration changes without finishing.

---

# 132. `isDestroyed`

Similarly, checking `isDestroyed` is often a band-aid; proper lifecycle scoping of operations is the fundamental solution.

---

# 133. SCREEN OFF / ON

If a feature depends on display state, verify:

- media playback behavior
- sensor listening
- UI animation timers

only where relevant to business logic.

---

# 134. DEVICE LOCK

Locking the device screen can background the application or alter resource availability.

Evaluate if the feature explicitly demands continuous execution.

---

# 135. PHONE CALL / INTERRUPTION

Media, camera, audio recording, and VoIP applications must handle interruption lifecycles (audio focus loss, call incoming).

If not applicable:

**NOT APPLICABLE**

---

# 136. LOW MEMORY

Do not rely solely on `onLowMemory()` for primary resource management.

However, inspect whether large in-memory caches are trimmed when the system signals low memory.

---

# 137. `onTrimMemory`

If implemented, verify that critical application state is not erroneously evicted along with disposable caches.

---

# 138. CACHE VS STATE

A resource cache is disposable.

User-generated state must never be lost merely because the application is releasing memory caches.

---

# 139. MULTI-WINDOW

An Activity can be visible without being in the `RESUMED` state (e.g. in multi-window mode when another window has focus).

If a critical feature pauses updates in `onPause`, check multi-window implications.

---

# 140. PICTURE-IN-PICTURE

If Picture-in-Picture (PiP) is supported:

- Activity lifecycle transitions
- UI control adjustments
- media player ownership
- return to full-screen mode

---

# 141. PiP ENTER/EXIT

Ensure configuration and UI changes during PiP transitions do not reset playback position or media state.

---

# 142. ORIENTATION LOCK

If the application locks screen orientation, verify whether this is done merely to bypass unresolved lifecycle defects.

Do not declare orientation locking as an inherent bug if it is a valid product requirement.

---

# 143. ACTIVITY RECREATION TEST

Where testing tooling permits, utilize recreation tests for critical Activities and Fragments.

---

# 144. `ActivityScenario.recreate()`

Validates configuration change handling.

However, it does NOT simulate true OS process death.

---

# 145. PROCESS DEATH TEST LIMITATION

Never mark:

```text
ActivityScenario.recreate() PASS
```

as evidence that:

```text
PROCESS DEATH PASS
```

These are fundamentally different system events.

---

# 146. DON'T KEEP ACTIVITIES

The developer option "Don't keep activities" helps reveal lifecycle issues.

However, it is not an identical simulation of process death (Application singleton and memory space remain intact).

Clearly document this distinction.

---

# 147. BACKGROUND PROCESS KILL TEST

If manual or instrumentation tests are used (e.g. killing the process via ADB while in the background), document the exact methodology.

Do not claim process death verification without actual simulation.

---

# 148. TEST MATRIX

For critical screens, produce:

| Scenario | Tested | Result |
|---|---|---|
| Rotation | | |
| Background / foreground | | |
| Navigation away / back | | |
| Activity recreation | | |
| Process death | | |
| Double tap | | |
| Slow response | | |

---

# 149. PROCESS DEATH MATRIX

For critical state:

| State | Source | Survives rotation | Survives process death | Expected |
|---|---|---|---|---|

---

# 150. RESOURCE LIFETIME MATRIX

| Resource | Created by | Expected lifetime | Cleanup | Risk |
|---|---|---|---|---|

Evaluate:

- listeners
- media players
- WebViews
- hardware sensors
- services
- callbacks
- coroutines

---

# 151. COROUTINE MATRIX

| Job | Scope | Should survive screen | Cancellation | Risk |
|---|---|---|---|---|

---

# 152. FINDING FORMAT

Every serious finding must include:

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

Feature:
Screen:
Lifecycle owner:
File:
Function/Class:
Relevant code:

Trigger:
Lifecycle transition:

Problem:

Evidence:

Lifecycle Flow:

Reproduction:

Expected behavior:

Actual behavior:

User impact:

Data impact:

Resource impact:

Root cause:

Recommended remediation:

Regression test:

Verification steps:

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

---

# 153. SEVERITY

Use:

## P1 - HIGH

- critical data loss
- core user flow crashes during routine lifecycle events
- severe memory or hardware resource leak
- major duplicate business transaction
- process death renders a critical screen unusable

## P2 - MEDIUM

- reproducible lifecycle bug on an important feature
- state loss with an available workaround
- stale UI updates or duplicate event callbacks

## P3 - LOW

- minor edge-case lifecycle issue
- localized UI reset with low user inconvenience

## P4 - IMPROVEMENT

- lifecycle architecture enhancement with no current runtime defect

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

---

# 154. CONFIDENCE

Use:

```text
HIGH
MEDIUM
LOW
```

HIGH:

verified at runtime or definitively proven by execution flow.

MEDIUM:

code strongly indicates the defect, but physical device verification is missing.

LOW:

depends on unverified platform or device-specific vendor behavior.

---

# 155. STATUS

Use:

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

---

# 156. LIFECYCLE EVENT LEVEL

For each finding, specify the triggering event:

```text
CONFIGURATION CHANGE
VIEW DESTROY
ACTIVITY DESTROY
PROCESS DEATH
BACKGROUND
FOREGROUND
NAVIGATION
SERVICE RESTART
OTHER
```

---

# 157. FALSE-POSITIVE PREVENTION

Before confirming a serious finding, verify:

1. true lifecycle owner
2. ViewModel scope
3. SavedStateHandle usage
4. database / DataStore persistence
5. Flow sharing parameters
6. coroutine scope cancellation
7. existing cleanup routines
8. navigation back stack retention
9. automated test coverage
10. underlying framework guarantees

Do not conclude an issue exists merely because `onDestroy()` cleanup is absent in that immediate file.

---

# 158. DO NOT CONFLATE LIFECYCLE CATEGORIES

Strictly distinguish:

```text
rotation / configuration change
```

from:

```text
process death
```

from:

```text
navigation away
```

from:

```text
app backgrounding
```

from:

```text
Activity finish
```

A fix addressing one scenario does not inherently resolve the others.

---

# 159. DO NOT MODIFY CODE

During the audit:

- do not move state around
- do not introduce ViewModels
- do not alter coroutine scopes
- do not add SavedStateHandle
- do not modify the Android Manifest
- do not refactor navigation

Complete the audit first.

---

# 160. OUTPUT - ANDROID_LIFECYCLE_BUG_AUDIT.md

Structure the final audit report:

## 1. Executive Summary

- lifecycle architecture overview
- top lifecycle failure risks
- critical state preservation status
- process-death readiness
- resource ownership health

## 2. Lifecycle Architecture Map

## 3. Activity Audit

## 4. Fragment Audit

## 5. Fragment View Lifecycle Audit

## 6. Compose Lifecycle Audit

## 7. ViewModel Scope Audit

## 8. Configuration Change Audit

## 9. State Restoration Audit

## 10. Process Death Audit

## 11. Navigation Lifecycle Audit

## 12. Coroutine Lifetime Audit

## 13. Flow / Observer Audit

## 14. Resource Ownership Audit

## 15. Background / Foreground Audit

## 16. Service Lifecycle Audit

## 17. External Activity / Permission Result Audit

## 18. Memory / Resource Leak Audit

## 19. Findings Summary

| ID | Severity | Lifecycle Event | Feature | Problem | Confidence | Status |
|---|---|---|---|---|---|---|

## 20. P1 Findings

## 21. P2 Findings

## 22. P3 Findings

## 23. P4 Improvements

## 24. Things Done Well

## 25. Lifecycle Test Coverage

## 26. Unknown / Not Verified

## 27. Remediation Roadmap

---

# 161. ROTATION SECOND PASS

For each critical screen, simulate:

```text
screen open
↓
partial user input completed
↓
device rotated
↓
new instance created
```

Ask:

- what state survived
- what tasks restarted
- what work duplicated
- what data was lost

---

# 162. PROCESS DEATH SECOND PASS

Then for the same screen:

```text
app backgrounded
↓
process killed by OS
↓
app restored via Recents
```

Ask:

- from where is state reconstructed
- what data ceases to exist
- can the destination reconstruct itself cleanly

---

# 163. NAVIGATE-AWAY PASS

Scenario:

```text
request starts
↓
user navigates away immediately
↓
response arrives
```

Ask:

> Who owns and handles the response now?

---

# 164. FAST BACK-FORWARD PASS

Simulate:

```text
Screen A
↓
Screen B
↓
Back
↓
Screen B
↓
Back
```

in rapid succession.

Look for:

- stale callbacks
- duplicate observers
- lingering retained state

---

# 165. BACKGROUND PASS

Scenario:

```text
operation initiates
↓
Home button pressed
↓
several minutes elapse
↓
app brought back to foreground
```

Check:

- active timers
- in-flight network requests
- authentication session validity
- progress indicators
- allocated hardware resources

---

# 166. SYSTEM KILL PASS

Ask:

> What critical user data exists exclusively in RAM?

Classify each identified item:

```text
safe to lose
user inconvenience
business data loss
not verified
```

---

# 167. DUPLICATE SUBSCRIPTION PASS

For each listener or observer:

```text
start
stop
start
stop
```

Verify that the active subscription count remains 1 or 0 as expected.

---

# 168. RESOURCE OWNER PASS

For each heavyweight resource, ask:

> Does the owner live for the exact duration that the resource is needed?

Specifically:

- media player
- camera
- WebView
- hardware sensors
- Bluetooth connections
- location updates

---

# 169. ASYNC COMPLETION PASS

For every asynchronous operation:

```text
operation starts
↓
owner destroyed
↓
operation completes
```

Verify outcome and error handling.

---

# 170. ERROR PASS

Re-run lifecycle scenarios when the asynchronous operation fails.

An error callback carries the exact same stale-owner hazards as a success callback.

---

# 171. LOGOUT PASS

Logout represents a critical lifecycle and state boundary.

Inspect:

- Activity back stack
- Fragment back stack
- ViewModel retention
- Flow collectors
- Background workers
- Running services

No private user data from the previous session must remain in memory or active streams.

---

# 172. FINAL QUALITY GATE

Before finalizing the audit, verify:

- rotation and process death are not conflated
- View lifecycle is strictly distinguished from Fragment lifecycle
- every coroutine has an identified, appropriate owner
- every registered listener has verified cleanup logic
- asynchronous findings evaluate what happens after navigation
- ViewModel is not treated as permanent durable storage
- SavedStateHandle is not treated as a database replacement for large payloads
- each process-death finding includes a concrete reconstruction scenario
- observer findings verify the LifecycleOwner
- `onResume` is not treated as synonymous with "app launched"
- `onPause` is not treated as synonymous with "app backgrounded"
- resource leak findings pinpoint the retained reference
- test reproduction notes distinguish configuration recreation from process death
- bugs and architectural improvements are strictly separated
- recommended remediation addresses root causes rather than symptoms

---

# FINAL RULE

I do not want a generic report stating:

> Use ViewModel and SavedStateHandle to fix lifecycle problems.

That is not a lifecycle audit.

I am looking for concrete issues such as:

```text
Fragment
↓
View created
↓
observer registered against Fragment lifecycle
↓
View destroyed
↓
Fragment remains on back stack
↓
Flow emits
↓
observer attempts to access destroyed binding
```

or:

```text
screen loads item A
↓
network request starts
↓
user navigates to item B
↓
A response arrives late
↓
shared ViewModel accepts result
↓
Screen B displays data belonging to item A
```

or:

```text
form input stored only in ViewModel
↓
app sent to background
↓
process killed by system
↓
user returns
↓
new ViewModel created
↓
form data permanently lost
```

or:

```text
listener registered in onStart
↓
not removed in onStop
↓
onStart executes again
↓
second listener added
↓
single event triggers business action twice
```

or:

```text
media player scoped to composable
↓
user navigates away
↓
composition disposed
↓
player released
↓
business requirement expected audio playback to continue
```

or:

```text
Activity starts long operation in lifecycleScope
↓
user rotates screen
↓
Activity destroyed
↓
coroutine cancelled
↓
operation never completes
↓
UI had already informed user it completed successfully
```

These are the concrete lifecycle bugs you must uncover.

Think through:

- owner
- lifetime
- recreation
- destruction
- process death
- cancellation
- restoration
- delayed completion
- duplicate registration
- resource cleanup

For every object or operation, ask:

> Who owns it?

> How long should it live?

> What happens when its owner disappears?

If evidence is insufficient:

**NOT VERIFIED.**

If the issue is merely an architectural enhancement without an active failure:

**P4 - IMPROVEMENT.**

It is far better to identify 7 genuine lifecycle bugs than to generate 70 generic recommendations.

The objective is a forensically sound audit from which every finding can directly translate into:

- lifecycle reproduction steps
- regression test
- exact ownership remediation
- process-death verification
- production validation

分类与标签

编程开发、安卓开发、生命周期、进程恢复、内存泄漏、协程管理

前往 AI 图片创作