【问题标题】:getting back a result from LauncherForActivityResult从 LauncherForActivityResult 取回结果
【发布时间】:2022-07-02 22:12:51
【问题描述】:

我有点卡在我正在编写的函数中。

我有一个函数调用我编写的另一个应用程序并需要返回一个布尔值

@Composable
fun callApp(
    //someparams
) : Boolean {
      //some code
}

经过一些逻辑,我通过以下方式启动我的应用程序:

val startForResult =
        rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            if (result.resultCode == Activity.RESULT_OK) {
               //here i need to set the return value of "callApp"
            }}

并通过

触发它
startForResult.launch(launchIntent)

问题是我看不到一种方法可以返回函数“callApp”在结果中生成的布尔值。 请注意,我不能使用 sharedPref/realm/代码本身外部的任何数据结构。

你能帮我理解一下吗?

【问题讨论】:

    标签: kotlin android-jetpack-compose activityresultcontracts


    【解决方案1】:

    最好只为意图完成进行回调,而不是从可组合/将其存储在某处返回值

    @Composable
    fun callApp(
        //someparams,
        onIntentFinished: (Boolean) -> Unit
    ) {
        // ...
        val startForResult =
            rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
                if (result.resultCode == Activity.RESULT_OK) {
                   //here i need to set the return value of "callApp"
                   onIntentFinished(true)
                }}
        // ...
    }
    

    对于其他解决方案,您可能希望传递一个对象来存储值

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 2016-07-07
      • 2015-07-15
      • 2018-09-24
      • 1970-01-01
      • 2018-01-01
      • 2021-04-26
      • 2021-11-10
      相关资源
      最近更新 更多