Best practice for calling an Android Activity with a Dependency Service

JeremyAlleyJeremyAlley USMember ✭✭

I have a PCL that defines an Interface (IPictureTaker). From a viewmodel within the PCL, I use the DependencyService to call my SnapPic method :
public interface IPictureTaker { void SnapPic(); }
In the view model:

public void ExecuteMethod() { IPictureTaker pictureTaker = DependencyService.Get<IPictureTaker>(); pictureTaker.SnapPic(); }

I implement the interface in the MainActivity of the Android Project and call activity.StartActivityForResult:

`public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity, IPictureTaker
//OnCreate omitted

    public void SnapPic()
        var activity = Forms.Context as Activity;

        var picker = new MediaPicker(activity);
        var intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions
            Name = "profile.jpg",
            Directory = "Krizzma"
        activity.StartActivityForResult(intent, 1);

protected override async void OnActivityResult(int requestCode, Result resultCode,
        Android.Content.Intent data)
        if (resultCode == Result.Canceled)

        var mediaFile = await data.GetMediaFileExtraAsync(Forms.Context);


        MessagingCenter.Send<IPictureTaker, Stream>(this, "pictureTaken", mediaFile.GetStream());



My Question: I would prefer to not use the MainActivity for this purpose as I plan to have multiple interfaces similar to this one and it feels wrong to have to handle which method was called inside of the OnActivityResult. I have tried creating a "TakePictureActivity" with the exact same code and the SnapPic method is called correctly, but OnActivityResult is called in the MainAcitivty. I believe this is because Forms.Context is the MainActivity. I have tried starting the TakePictureActivity from within the SnapPic method, but was unsuccessful and it also does not seem to be the correct way to go about it.

Any suggestions on how to properly implement this would be appreciated.

Best Answer


  • JeremyAlleyJeremyAlley USMember ✭✭

    Thanks, I'll look into your blog post. I actually solved my issue by using the XLabs implementation.

  • vigneshmvigneshm USMember

    @AdamKemp Hi, Im preety new to xamarin. I ve read your blog post and i try to implement.Unfortunately I got this error.How can i Solve this.?


  • adamkempadamkemp USInsider, Developer Group Leader mod

    It looks like your definition of IAsyncActivityResult is not public. That interface needs to have the same access scope as the StartActivityForResultAsync method. If the method is public then the interface must be as well.

  • vicky_mikvicky_mik INMember

    @adamkemp I have implemented your FragmentmentBase class in my application. Here FragmentBase class is wind up after calling the method startActivity(asyncActivityResult.RequestCode) which is called inside the Task StartActivityForResultAsyncCore(Action startActivity, CancellationToken cancellationToken) method .Did i missed anything.?

    Here is my Code

    async Task<Android.Net.Uri> IImagePicker.GetImages()
    var intent = new Android.Content.Intent(Android.Content.Intent.ActionGetContent);
    var result = await StartActivityForResultAsync(intent);
    if (result.ResultCode == Result.Ok)
    // Get the image from data
    return uriImage;


  • adamkempadamkemp USInsider, Developer Group Leader mod

    @vicky_mik I don't understand your question. Is the code not working? Can you give more detail about what is happening? Can you post a complete example?

  • Kisu_reddyKisu_reddy INMember

    @adamkemp Is it possible to use dependency service for non xamarin forms like xamarin android?

  • adamkempadamkemp USInsider, Developer Group Leader mod
    You can always reference the Xamarin.Forms nuget and only use that class, but it seems like a huge dependency just for that.

    There are many implementations of the same pattern that you can use. Look for dependency injection frameworks in nuget or elsewhere. It’s also a pretty simple concept to write yourself if needed. Remember, Xamarin.Forms is open source now so you can go look at how it’s implemented.
Sign In or Register to comment.