简体   繁体   English

onActivityResult() 可以在 onCreate() 之前调用吗?

[英]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()之前调用,但让我们考虑一下这种极端情况:

  1. activity A is running and starts activity B for a result活动 A 正在运行并启动活动 B 以获得结果
  2. activity B opens活动 B 打开
  3. the device is running low on memory, so it destroys activity A该设备在 memory 上运行不足,因此它破坏了活动 A
  4. activity B finishes with a result活动 B 以结果结束

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想要将结果返回给您的应用程序时,会发生以下情况:

  • Android creates a new OS process for your app (because it killed yours and needs a new one) Android 为您的应用程序创建一个新的操作系统进程(因为它杀死了您的应用程序并需要一个新进程)
  • Android instantiates the Application instance (or your app's custom one) and calls onCreate() on that Android 实例化Application实例(或您的应用程序的自定义实例)并在其上调用onCreate()
  • Android instantiates a new instance of the Activity that will get the result (the one that called startActivityForResult() ) Android 实例化一个新的Activity实例,它将获得结果(调用startActivityForResult()的那个)
  • Android calls onCreate() on the new Activity . Android 在新的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() )。
  • Android calls onStart() on the new Activity Android 在新Activity上调用onStart()
  • Android calls onActivityResult() on the new Activity Android 在新Activity上调用onActivityResult()
  • Android calls onResume() on the new Activity Android 在新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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM