How to handle the result of StartActivityForResult in Forms

In the Android specific code of my Xamarin.Forms project, I use the following code to request the user to turn Bluetooth on:

const int RequestEnableBt = 2;
var context = Forms.Context;
var activity = (Activity)context;
var intent = new Intent(BluetoothAdapter.ActionRequestEnable);
activity.StartActivityForResult(intent, RequestEnableBt);

The user can either accept or deny the request.
In Xamarin.Android the result is handled in Activity.OnActivityResult().
Is there a way to handle this in Xamarin.Forms?

Posts

  • AntaoAlmadaAntaoAlmada PTMember ✭✭

    It works! Thanks a lot for the tip.

  • manishchoudharymanishchoudhary USMember ✭✭

    Thanks @MarkFeldman

  • Richard_WRichard_W USMember ✭✭

    This was very helpful, and it works perfectly. Couple of things worth adding, for anyone starting out. I needed to use

    context.StartActivity(Intent.CreateChooser(intent, "Suitable title"));  
    

    instead of just passing the intent in directly. Also, to access the MainActivity context, I needed to save that as a static member in the MainActivity class, like this:

        internal static MainActivity Instance { get; private set; }
    
        protected override void OnCreate(Bundle bundle)
        {
             Instance = this;   
        ...
    }
    

    the Instance can then be used as either Context or cast to an Activity:

    Context context = MainActivity.Instance;
    MainActivity activity = (MainActivity)context;
    

    The only other thing is, for anyone who doesn't understand how to inject this into their shared code, is that you need to decorate the platform project namespace with

    [assembly: Xamarin.Forms.Dependency(typeof(BluetoothService))], 
    

    and in the PCL you will need to get an instance of the BluetoothService class like this:

    BluetoothService service = DependencyService.Get<IBluetoothService>();
    

    But this really shows just how easy it is to call a platform method from the PCL.

  • Thanks!!, works like a charm.

  • @MarkFeldman great minds - we practically wrote the same code independently, even to the same names. In addition i added a Filter - so i only get the callbacks if the resultCode and/or the requestCode match. that is usefull since i can add one to catch a good result and a different one to catch failed result for example. They can also if match and execute remove themselves and the others they are chained with - so its sort of a run once deregister. i of course have to deregister manually in case it does not run at all which is part of my usual dispose in the classes that adopt this approach.

    but consider two actions - one waits for Result.Ok the other for Result.Cancelled.

    If either of them run - since they are chains they will Degreister themselves and the other automatically.

  • sscy09sscy09 USMember

    Hi,
    I was trying to implement you code but I have an error when I subscribe to activity results:

        public ActivityResultListener(MainActivity activity)
        {
            // subscribe to activity results
          **  activity.ActivityResult += OnActivityResult;**
        }
    

    MainActivity doesn't contain a definition for ActivityResult
    What should I change it?

    Thank you

Sign In or Register to comment.