Android Katha: onActivityResult is Deprecated. Now What?

11 / Jul / 2022 by Kuldeep Singh 0 comments

 

fun openSecondActivityForResult() {
    val intent = Intent(this, SecondActivity::class.java)
    startActivityForResult(intent, REQUEST_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)    
    
    if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
        // Do operations
    }
}

fun openSecondActivityForResult() {
    val intent = Intent(this, SecondActivity::class.java)
    activityResultLauncher.launch(intent)
}

// We can do the assignment inside onAttach or onCreate
var activityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
    // There are no request codes

    if (result.resultCode == resultCode1 && result.data != null) {
        // Do operations
    } else if (result.resultCode == resultCode2) {
        // Do operations
    }
}
// We can do the assignment inside onAttach or onCreate
var activityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
    // There are no request codes    

    if (result.resultCode == resultCode1 && result.data != null) {
        // Do operations
    } else if (result.resultCode == resultCode2) {
        // Do operations
    }
}
val intent: Intent = Intent(this, SecondActivity::class.java) 
activityResultLaunch.launch(intent)
val intent = Intent()
setResult(FirstActivity.resultCode1, intent)
finish()

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *