0

I am updating some Xamarin.Android code to use the newer AndroidX APIs. The old way to launch an activity in my project was like this:

var intent = new Intent(this.Activity, typeof(SomeActivity));
intent.PutExtra("someVariableA", a);
intent.PutExtra("someVariableB", b);
StartActivityForResult(intent, 0);

The new way is with ActivityResultLauncher object:

activityResultLauncher.Launch(intent);

But how do I pass the request code? (second parameter in StartActivityForResult)

Drake
  • 2,679
  • 4
  • 45
  • 88
  • 1
    Purpose of `requestCode` was to differentiate results in `onActivityResult`, with `ActivityResultLauncher` API it becomes an internal detail and you no longer have to handle it - when result comes it gets passed down to `ActivityResultCallback` you defined when registering your `activityResultLauncher`. – Pawel Aug 17 '22 at 23:21
  • For details of this problem, please refer to https://stackoverflow.com/questions/66541013/how-to-use-activityresultcontract-in-xamarin-android. – Hongxin Sui-MSFT Aug 18 '22 at 08:07

1 Answers1

0

First you do not need onActivityResult(). That way was old.You now have launchers for specific purposes.Create a function like this:

ActivityResultLauncher<String> imageActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.GetContent(),
            uri -> 
               //do something with uri
            });

And you want to launch this, just write:

imageActivityResultLauncher.launch("image/*");

For more details refer

https://stackoverflow.com/a/63654043/12555686

Harshil
  • 162
  • 11