Created
February 11, 2014 15:53
-
-
Save AArnott/8937612 to your computer and use it in GitHub Desktop.
A C# Xamarin.Android activity base class that offers StartActivityForResultAsync as a much simpler means to kick off activities and resume execution when they are done.
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
public abstract class ActivityBase : Activity { | |
private int activityResultRegistrationCounter = 10000; | |
private Dictionary<int, TaskCompletionSource<Tuple<Result, Intent>>> activityResultRegistrations = new Dictionary<int, TaskCompletionSource<Tuple<Result, Intent>>>(); | |
/// <summary> | |
/// Starts another activity and allows awaiting on its return. | |
/// </summary> | |
/// <param name="intent">The intent that should be used to launch the new activity.</param> | |
/// <returns>A task that completes with the finishing of the activity, providing the result of the activity.</returns> | |
/// <example> | |
/// var result = await this.StartActivityForResultAsync(Intent.CreateChooser(intent, "Select picture")); | |
/// if (result.Item1 == Result.Ok) { | |
/// // User chose a picture. Get the Uri | |
/// Android.Net.Uri imageSource = result.Item2.Data; | |
/// } | |
/// </example> | |
public Task<Tuple<Result, Intent>> StartActivityForResultAsync(Intent intent) { | |
int requestCode = activityResultRegistrationCounter++; | |
var completionSource = new TaskCompletionSource<Tuple<Result, Intent>>(); | |
this.activityResultRegistrations[requestCode] = completionSource; | |
this.StartActivityForResult(intent, requestCode); | |
return completionSource.Task; | |
} | |
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { | |
base.OnActivityResult(requestCode, resultCode, data); | |
TaskCompletionSource<Tuple<Result, Intent>> completionSource; | |
if (this.activityResultRegistrations.TryGetValue(requestCode, out completionSource)) { | |
this.activityResultRegistrations.Remove(requestCode); | |
completionSource.SetResult(Tuple.Create(resultCode, data)); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment