You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android P预览版中onStart先于onActivityResult执行的疑问

Android P (Pie) onActivityResult Lifecycle Order Change: Bug or Feature?

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 after onStart() but before onResume() when returning to an Activity from startActivityForResult().
  • 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 when onActivityResult() 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 in onStart() before onActivityResult() runs.
  • If updating UI based on the result, double-check that views are fully initialized (e.g., avoid calling findViewById() in onActivityResult() unless you’ve confirmed onStart() has executed).

内容的提问来源于stack exchange,提问作者Fadi Selim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:23:25