Can't override OnActivityResult from Renderer
I am creating a renderer to open a native Android activity from a Xamarin.Forms app. In my renderer, I have:
public class CaptureBarcodeRenderer : PageRenderer
{
private Activity _activity;
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
_activity = Context as Activity;
Intent intent = new Intent(_activity, typeof(CaptureBarcode));
_activity.StartActivityForResult(intent, 0);
//activity.StartActivity(intent);
}
}
I am using the example from xamarin-forms-samples for this. As you can see from the action I run, I grab the barcode. Unfortunately I am unable to notify the page that opened this operation that the barcode has been captured. If I can use the result, I can notify the calling page (since I have access to that here) that we have a scan and then PopAsync () is called from within.
Since the calling page is a ContentPage, I cannot pass it as part of the data in the intent because it is not serializable.
Question How to handle OnActivityResult when it exists in Context and I cannot override methods to do this or subscribe to events.
I want to open an activity for the result when I get the activity result. I want to close the barcode action I started and pop the page from the navigation stack.
Any help?
source to share
I have this job, but I was not very happy with how it turned out.
In my MainActivity class, I have created a static property (BarcodeView) that represents the page from the form side. In my custom render, I set this variable to e.NewProperty.
In my MainActivity I am overriding OnActivityResult
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
BarcodeView.DoScan(data.GetStringExtra("code"));
}
Now when I scan this method hits. In the Scan operation, I finish () and then when I receive my DoScan () I find navigation to get rid of the custom Renderer view.
source to share
I just ran into this problem, so thanks for posting your answer. It helped me a lot. I made a small change as in my case I need other pages to return the activity results. In MainActivity.cs I added the following:
public class ActivityResultEventArgs : EventArgs
{
public int RequestCode { get; set; }
public Result ResultCode { get; set; }
public Intent Data { get; set; }
public ActivityResultEventArgs() : base()
{}
}
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
public event EventHandler<ActivityResultEventArgs> ActivityResult = delegate {};
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new App ());
}
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) {
ActivityResult (this, new ActivityResultEventArgs {
RequestCode = requestCode,
ResultCode = resultCode,
Data = data
});
}
}
Then each PageRender can receive results from events like this:
protected override void OnElementChanged (ElementChangedEventArgs<Page> e)
{
base.OnElementChanged (e);
var activity = this.Context as MainActivity;
if (e.OldElement != null)
{
activity.ActivityResult -= HandleActivityResult;
}
if (e.NewElement != null)
{
activity.ActivityResult += HandleActivityResult;
}
}
private void HandleActivityResult(object sender, ActivityResultEventArgs e)
{
Console.WriteLine (string.Format ("Activity result is {0}", e.ResultCode));
}
source to share