简体   繁体   English

Android:返回结果的活动会关闭应用程序

[英]Android: Activity for result pressing back closes the app

In my app, I am opening a Contact activity to pick a contact using the new Activity Result API like this在我的应用程序中,我正在打开一个联系人活动来使用新的活动结果 API 来选择一个联系人,如下所示

registerForActivityResult(ActivityResultContracts.PickContact()) { it ->
            viewModel.setContact(it) 
        }.launch(null)

The Contact activity opens, I can retrieve the contact, all this works just fine.联系人活动打开,我可以检索联系人,这一切都很好。 But while in the Contact activity, pressing the back button (either the android back button or the back arrow at the top), instead of closing just the contact activity, it exits my app.但是在 Contact 活动中,按下后退按钮(android 后退按钮或顶部的后退箭头),而不是仅关闭联系活动,它会退出我的应用程序。

I found fixes online for this issue, but only for the old startActivityForResult and onActivityResult APIs.我在网上找到了针对此问题的修复程序,但仅适用于旧的startActivityForResultonActivityResult API。 Is there a way I can change the behavior of the back button when opening an activity for result for the new API as well?在为新 API 的结果打开活动时,有没有一种方法可以更改后退按钮的行为?

Edit: Noticed the behavior both on emulator and on physical device编辑:注意到模拟器和物理设备上的行为

Adrian kindly provided me with a sample of his project.阿德里安好心地向我提供了他的项目样本。 This answer is based on the results of debugging the sample project.此答案基于调试示例项目的结果。

There is one "invisible" issue in the code that is caused as the result of interoperability of Java and Kotlin languages.由于 Java 和 Kotlin 语言的互操作性,代码中存在一个“不可见”问题。 When we call Java function from Kotlin code we get return type of T!当我们从 Kotlin 代码调用 Java 函数时,我们得到T!返回类型T! - where T! - 哪里T! means "T or T?".意思是“T 还是 T?”。

Java allows returning null values as we wish. Java 允许按照我们的意愿返回null值。 In order for Kotlin to know that the value is explicitly optional Java programmers must use @Nullable annotation.为了让 Kotlin 知道该值是显式可选的,Java 程序员必须使用@Nullable注释。 Kotlin will pick it up and return T? Kotlin 会捡起来返回T? instead.反而。

Since registerForActivityResult is Java function we get in return, for registerForActivityResult(ActivityResultContracts.PickContact()) , result of type Uri!由于registerForActivityResult是我们返回的 Java 函数,对于registerForActivityResult(ActivityResultContracts.PickContact())Uri!类型的结果Uri! . .

Again, Uri!又是Uri! means "Uri or Uri?"意思是“乌里还是乌里?” - "value or null". - “值或空”。

To handle this value in a Kotlin style we can safely unwrap it by using question mark ?要以 Kotlin 风格处理这个值,我们可以使用问号? :

activityResultLauncher = registerForActivityResult(ActivityResultContracts.PickContact()) { 
    it?.let { contactUri ->
        viewModel.setContact(contactUri)
    }
}

More about notation for platform types 有关平台类型符号的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下返回按钮时,结果活动关闭主要活动 - Result activity closes main activity when pressing back button Android webview中的ReactJS应用程序 - 按后退按钮关闭应用程序 - ReactJS app in Android webview - Pressing back button closes app 按下向后导航键时,Android Webview应用程序强制关闭 - Android Webview app force closes while pressing the back navigation key 两次按返回按钮可关闭活动 - pressing back button two times closes activity 按下后退按钮时,应用程序将关闭,同时在单个活动中使用导航抽屉中的片段 - On pressing back button app closes while using fragments in navigation drawer in single activity Android:键盘关闭后,带有EditText的DrawerLayout按下时会关闭应用程序 - Android: DrawerLayout with EditText after keyboard dismissed closes app when pressing back 按返回按钮时活动未发送结果 - activity not sending result when pressing back button 活动2中的“后退”按钮关闭应用程序,而不是在Android 7中进入活动1 - Back button from activity 2 closes down the app instead of going to activity 1 in Android 7 为什么在cordova内部按下后退按钮WebView会关闭应用程序? - Why pressing back button inside of cordova WebView closes app? 尝试移至上一个片段时,应用程序会在按返回按钮时关闭 - App closes on pressing back button when trying to move to previous fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM