Along with androidX support StartActivityForResult and OnActivityResult got dprecated inside Fragment. Here we can see the one of the alternate approach for this deprecated api's in xamarin android.
Below code snippet explains the handling the above mentioned deprecated API's:
In my previous post explained Android Keystore based encryption and decryption, How to implement Dependency injection in xamarin form using AutoFac, ReOrder the list items by drag and drop in xamarin android, Custom Image Gallery Control using Xamarin Form
-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AndroidX.Activity.Result; | |
using AndroidX.Activity.Result.Contract; | |
[Activity(Label = "EditImageActivity", MainLauncher = false)] | |
public class MyActivity : Activity | |
{ | |
private ActivityResultCallback _activityResultCallback; | |
private ActivityResultLauncher _activityResultLauncher; | |
public static int _requestCode; | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
_activityResultCallback = new ActivityResultCallback(this); | |
_activityResultLauncher = RegisterForActivityResult(new ActivityResultContracts.StartActivityForResult(), _activityResultCallback); | |
} | |
private void OpenGallery() | |
{ | |
//var galleryIntent =.. //create your intent | |
_requestCode = "1001"; //flag to handle the multiple intent request | |
_activityResultLauncher.Launch(galleryIntent); | |
} | |
public void MyActivityResultReceived( int resultCode, Intent data) | |
{ | |
if ((_requestCode == "1001") || (resultCode == (int)Result.Ok)) | |
{ | |
//handle the result | |
} | |
} | |
class ActivityResultCallback : Java.Lang.Object, IActivityResultCallback | |
{ | |
MyActivity _myActivity; | |
public ActivityResultCallback(NotesFragment myActivity) | |
{ | |
_myActivity = myActivity; //initialise the parent activity/fragment here | |
} | |
public void OnActivityResult(Java.Lang.Object result) | |
{ | |
var activityResult = result as ActivityResult; | |
_myActivity.MyActivityResultReceived(activityResult.ResultCode, activityResult.Data); //pass the OnActivityResult data to parent class | |
} | |
} | |
} |
Happy coding!!
Hello,
ReplyDeleteActivityResultCallback class not found.
However IActivityResultCallback interface exists
Do I need to add any references?
OK. The class is there at the end of the code.
ReplyDeleteThanks
I'm upgrading one of my apps to android API 33 and I'm having some issues with this code that I can't resolve. The line " _activityResultLauncher = RegisterForActivityResult(new ActivityResultContracts.StartActivityForResult(), _activityResultCallback);" causes the IDE error "The name 'RegisterForActivityResult' does not exist in the current context". I'm using Visual Studio 2022 v17.3.6. I can't even test the code until I resolve this. Can anyone here help me?
ReplyDeleteThis comment has been removed by the author.
DeleteDid you add required namespace, as suggested in this sample code
Delete