Android P预览版中onStart先于onActivityResult执行的疑问
Great question—this is exactly the kind of behavior shift you might encounter when testing early platform previews, and it’s easy to confuse with a bug at first glance. Let’s break this down clearly:
1. This is an Intentional Platform Change, Not a Bug
The adjusted lifecycle order (onStart() → onActivityResult() → onResume()) that you’re seeing in Android P preview builds (and later the official Android 9 Pie release) is a deliberate change from the Android team, not an oversight.
2. Rationale Behind the Shift
Prior to Android P, the order (onActivityResult() → onStart() → onResume()) could lead to edge cases, especially with launch modes like singleTask (your Activity A’s mode) or singleTop:
- If Activity A was in a stopped state when returning from B, processing the result before
onStart()meant you might be interacting with uninitialized views or state that hadn’t been restored yet. - The new order aligns with core lifecycle semantics:
onStart()signals that the Activity is about to become visible and has restored its basic state. Processing the result after this ensures your app is in a stable state to handle the returned data.
3. Official Documentation & Confirmation
While early preview docs might have been lagging, the official Android 9 Pie documentation now clarifies this updated order:
onActivityResult()is guaranteed to execute afteronStart()but beforeonResume()when returning to an Activity fromstartActivityForResult().- This change is also noted in the Android 9 behavior changes guide, framed as a consistency improvement for task stack operations and lifecycle reliability.
4. Actionable Advice for Your Code
Since your app uses targetSdkVersion 23, you’ll want to adjust any logic that relied on the old order:
- Avoid assuming
onStart()has already run whenonActivityResult()is called (this was the case pre-P, but not anymore). - Move view initialization or state restoration code needed for handling results into
onCreate()or ensure it’s completed inonStart()beforeonActivityResult()runs. - If updating UI based on the result, double-check that views are fully initialized (e.g., avoid calling
findViewById()inonActivityResult()unless you’ve confirmedonStart()has executed).
内容的提问来源于stack exchange,提问作者Fadi Selim

