xamarin/android-activity-controller

Name: android-activity-controller

Owner: Xamarin

Description: null

Created: 2016-12-07 17:57:18.0

Updated: 2018-05-09 11:39:41.0

Pushed: 2017-10-07 21:11:00.0

Homepage: null

Size: 52

Language: C#

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Xamarin.Android ActivityController

The ActivityController makes managing Android Activities more .NET friendly.

Traditionally, launching subsequent Activities and waiting for them to return their results has been somewhat painful. With the ActivityController you can use async/await by starting activities through the StartActivityForResultAsync (..) method.

You can use ActivityController as a replacement for AppCompatActivity. The underlying lifecycle of your activity is handled for you.

Your ActivityController subclass can override some of the typical methods you would expect in an Activity. It must be associated with a subclass of ControllerActivity<TController>. Here is a boiler plate implementation:

ivity(MainLauncher = true, Label = "Your Activity", Theme = "@style/Theme.AppCompat")]
ic class MainActivity : ControllerActivity<MainActivity.MainController>

public class MainController : ActivityController
{
    protected override void OnCreate(Android.OS.Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.FirstLayout);

        // Your code            
    }
}

Now you are ready to call StartActivityForResultAsync (..) from your controller:

c void Button_Click(object sender, EventArgs e)

var contactPickerIntent = new Intent(Intent.ActionPick, ContactsContract.CommonDataKinds.Phone.ContentUri);

var result = await StartActivityForResultAsync(contactPickerIntent);

var contactUri = result?.Data?.Data;

// Get Contact Name from the ContentResolver
// var displayName = ...

if (contactUri != null)
    Toast.MakeText(Activity, "You Picked: " + displayName, ToastLength.Long).Show();

You can also access the instance of the underlying AppCompatActivity directly via the Activity property of your ActivityController, as seen in the snippet above.

Finally, if you need to, you can also alter your subclass of ControllerActivity directly to override more methods and make other changes.

ivity(Theme = "@style/Theme.AppCompat")]
ic class MainActivity : ControllerActivity<MainController>

public override bool OnTouchEvent(Android.Views.MotionEvent e)
{
    // Do something interesting

    return base.OnTouchEvent(e);
}

Helpers

To make some common tasks easier, ActivityController also contains some helper methods which construct the appropriate Intent and returns a strongly typed version of ActivityResult with more useful properties.

The helper methods include:

result = await PickContactAsync ();
contactUri = result.SelectedContactUri;
result = await PickPhotoAsync ("Title");
stream = result.GetMediaStream ();

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.