Android Kotlin - 在 Recyclerview 中 registerForActivityResult [英] Android Kotlin - registerForActivityResult inside Recyclerview
问题描述
我试过了:
val getUpdates = (context as Activity).registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
}
}
在 RecyclerView 中并获取:未解析的引用:registerForActivityResult
inside RecyclerView and get: Unresolved reference: registerForActivityResult
在 RecyclerView 里面是不可能的吗?
Is it not possible inside RecyclerView?
推荐答案
您尝试执行的操作将导致崩溃.结果回调必须在Activity创建时注册(即作为Activity的字段/变量)
What you're attempting to do will result in a crash. The result callback must be registered at Activity creation (ie as a field/variable of the Activity)
每次创建 Activity 时都必须无条件注册回调,即使启动其他 Activity 的逻辑仅基于用户输入或其他业务逻辑发生.
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.
有关详细信息,请参阅文档
这篇关于Android Kotlin - 在 Recyclerview 中 registerForActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!