OnActivityResult method is deprecated, what is the alternative?

前端 未结 5 536
情话喂你
情话喂你 2021-01-01 10:33

Recently I faced the onActivtyResult is deprecated. what should we do for handle it?

any alternative introduced for that?

5条回答
  •  醉梦人生
    2021-01-01 10:48

    From now, startActivityForResult() has been deprecated so use new method instead of that.

    Kotlin Example

    fun openActivityForResult() {
             startForResult.launch(Intent(this, AnotherActivity::class.java))
    }
    
    
    val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { 
    result: ActivityResult ->
        if (result.resultCode == Activity.RESULT_OK) {
            val intent = result.data
            // Handle the Intent
            //do stuff here
        }
    }

提交回复
热议问题