Xamarin Android - Activity

In Android, an Activity represents a screen. It's one of the fundamental components of Android. Other components include: Service, BroadcastReceiver and ContentProvider.

Whenever you are interacting visually with an android app, you are interacting with an activity. Because even if it's a widget like button or edittext, all those get hosted in an activity or a fragment.

And fragments themesevles are just sub-activities. Fragments get hosted in the activity too.

An android application may have one or more activities. One or more pages/screens. The simplest just require a single activity.

So for instance if you want a an app that will have 5 pages, you can create 5 activities, or maybe two activities and 3 fragments, or 3 activities and 2 fragments or something like that.

So android apps must always have activities.

Activities belong to Android.App package, or namespace as in the case of C# language which are using with our Xamarin Android.

The base activity class definition has two Activity constructors, one public:

public Activity();

and another protected:

protected Activity(IntPtr javaReference, JniHandleOwnership transfer);

However, you won't create an activity using these constructors.

Instead you will create an activity by deriving from Android.App.Activity:

public class MainActivity : Activity{}

This has created an activity class called MainActivity.

So with that we have already created an empty but full activity.

However, it's not very useful. Activities represent screens and probably you want to show something to users. Say some information or some buttons or something like that.

But activities are not just ordinay classes. These are special classes and are actually a fundamental component in android platform as a whole. In fact they are at the heart of android development.

Activities do these among other things:

  1. Represent the a full screen in android app.
  2. They host user interface components like buttons, textviews, edittexts, listviews etc.
  3. They host fragments. Fragments are sub-activities.
  4. Each activity contains its own data that it acts on.

One of the special and unique properties of activities is their support for lifecycle callbacks. These are methods that get called by the system at various stages during the construction of the activity.

For example at the activity creation stage, the OnCreate() callback gets called. At the destruction face, the OnDestroy() gets called and more and more.
Some of these callbacks include;

  • OnCreate()
  • OnStart()
  • OnResume()
  • OnPause()
  • OnStop()
  • OnRestart()
  • OnDestroy()

Check our activity lifecycle callbacks tutorial to observe them in detail.

So normally with activities, we override any, some or all of these methods. It all depends on what the activity is actually doing.

For instance, majirity of the time we want to show user interface widgets on the screen. Normally we prepare and initialize these inside the OnCreate() method.

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
        }

Android activities typically use XML markup to define user interface. The process of converting this XML specification into java object that can be rendered is called inflation.

The layout inflation for an activity are typically done also inside the OnCreate() method. Here are what is typically done in the OnCreate() method:

  1. First you call the base(Android.App.Activity)'s implementation of the OnCreate() method:
    base.OnCreate(bundle);
    
  2. Second we inflate the layout resource via the SetContentView() method:
    SetContentView (Resource.Layout.Main);
    

Then after these two you can now initialize your widgets like buttons, textview etc. These widgets get defined in the XML layout.

Let's define a textview in Main.xml layout file under resources:

 <TextView
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myTxt" />

You paste this inside your layout, not to replace the layout but inside. Clearly you can see this is a textview with an id myTtxt.

So now we can come inside our OnCreate() and reference that textview:

TextView nameTxt = FindViewById<TextView>(Resource.Id.myTxt);

Now that gives us textview object that we can use to display text programmatically.

nameTxt.Text = "Hello World";

Let's look at a full example.

Well here's the project structure:
Xamarin Android Activity Hello World

Define the Layout

This is the definition for the Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#009968"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 <TextView
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myTxt" />
 </LinearLayout>

Create the activity

using Android.App;
using Android.Widget;
using Android.OS;

namespace Centaurus
{
    [Activity(Label = "Centaurus", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            TextView nameTxt = FindViewById<TextView>(Resource.Id.myTxt);
            nameTxt.Text = "Hello World";
        }
    }
}

And this is what we get:

Xamarin Android Activity Example