which class I should use for Activity result callback array?

965 Views Asked by At

I am trying to use the new ActivityResultLauncher Intent for activity result, when I make the second parameter callback (ActivityResultCallback) Android Studio asks me which Class I want to import for the result and gives me 3 options.

enter image description here

I don´t know which one I need to use and which one is for the "old" method (startActivityForResult).

I tried the 3 ways and they all seems to pass, but I get different errors in the Logcat for every option.

What is the correct choice?

This is the code:


    ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();
                    doSomeOperations();
                }
            }
        }
    );
1

There are 1 best solutions below

0
On

It should be androidx.activity.result

Sample code (in Kotlin):

val startForResult = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()) { 
        result: androidx.activity.result.ActivityResult ->
            when (result.resultCode) {
                Activity.RESULT_OK -> {
                    // Your code
                }
                Activity.RESULT_CANCELED -> {
                    // Your code
                }
            }
        }