[英]Can onActivityResult() be called before onCreate()?
Can onActivityResult()
theoretically be called before onCreate
?理论上可以在
onCreate
() 之前调用onActivityResult()
吗? I know it can be called before onResume()
, but let's consider this extreme case:我知道它可以在
onResume()
之前调用,但让我们考虑一下这种极端情况:
What happens now?现在会发生什么? Does activity A get re-created before receiving the result, or does it receive the result before
onCreate()
?活动 A 是在收到结果之前重新创建,还是在
onCreate()
之前收到结果? Is this guaranteed to be in the same order each time?这是否保证每次都以相同的顺序排列?
You will commonly see this if you are opening an app to scan a barcode or take a photo or video.如果您打开应用程序扫描条形码或拍摄照片或视频,您通常会看到这一点。 The
Activity
you launch to do that requires a lot of resources so Android kills the OS process hosting your app.您为此启动的
Activity
需要大量资源,因此 Android 会杀死托管您的应用程序的操作系统进程。
When the Activity
you launched wants to return the result to your app, the following occurs:当您启动的
Activity
想要将结果返回给您的应用程序时,会发生以下情况:
Application
instance (or your app's custom one) and calls onCreate()
on that Application
实例(或您的应用程序的自定义实例)并在其上调用onCreate()
Activity
that will get the result (the one that called startActivityForResult()
) Activity
实例,它将获得结果(调用startActivityForResult()
的那个)onCreate()
on the new Activity
. Activity
上调用onCreate()
。 The Bundle
passed to onCreate()
in this case is the Bundle
most recently used to the save the Activity
instance state (from onSaveInstanceState()
).onCreate()
的Bundle
是最近用于保存Activity
实例 state 的Bundle
(来自onSaveInstanceState()
)。onStart()
on the new Activity
Activity
上调用onStart()
onActivityResult()
on the new Activity
Activity
上调用onActivityResult()
onResume()
on the new Activity
Activity
上调用onResume()
Those case Use那些案例使用
registerForActivityResult()
Activity Result APIs decouple the result callback from the place in your code where you launch the other activity.
活动结果 API 将结果回调与代码中您启动其他活动的位置分离。 As the result callback needs to be available when your process and activity are recreated, the callback must be unconditionally registered every time your activity is created, even if the logic of launching the other activity only happens based on user input or other business logic
由于重新创建流程和活动时结果回调需要可用,因此每次创建活动时都必须无条件注册回调,即使启动其他活动的逻辑仅基于用户输入或其他业务逻辑发生
more details refer Getting a result from an activity更多详细信息请参阅从活动中获取结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.