ice spirit
ice spirit

Reputation: 1535

startActivityForResult(android.content.Intent, int)' is deprecated

startActivityForResult(intent,3021) 

I am using this since long time, is this method is deprecated now?

Upvotes: 16

Views: 24436

Answers (1)

hushed_voice
hushed_voice

Reputation: 3608

It is indeed deprecated. I tried to figure the new way out and thought I want to share it here.

Now instead of overriding onActivityResult for all callbacks and checking for our request code, we can register for each call back separately by using registerForActivityResult which takes in an ActivityResultContracts. IMO this is a way better approach than the previous way.

Here is an example of starting an activity for a result:

val previewRequest =
           registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
    if (it.resultCode == RESULT_OK) {
        val list = it.data
        // do whatever with the data in the callback
    }
}

Now instead of StartActivityForResult we use

val intent = Intent(this, PreviewFullscreenActivity::class.java)
intent.putStringArrayListExtra(AppConstants.PARAMS.IMAGE_URIS, list)
previewRequest.launch(intent)

Upvotes: 12

Related Questions