Xamarin for Android #4–Weirdness Explained

By September 18, 2013Xamarin
[Please see the Table of Contents for this series.]

.

SecondActivityMost tutorials and books on frameworks and systems insist on giving you “all the background you need to really understand the system” because, they claim, without that, you won’t understand how it all fits together.  I find, on the other hand, that what I want, is to get busy creating applications, and then understand the architectural underpinnings later, as I go.

That said, Android is so weird that there are a few concepts you really do need to grok before you can go much further than “Hello World.”  These include

  • Activities

  • Intents

  • Services

  • Contexts

  • The Manifest

Android is weird because there is no “there” there.  There is no application per se, with specific boundaries that begins at a beginning and ends at a conclusion.  Instead, there are Activities which exist almost independently of an application and there are Intents, each of which is essentially a request for an Activity bundled up with data.  An Activity can fire off an Intent which will cause an Activity from some other application to fire up and fulfill the intent.  Very odd.

Services are like Activities except they last longer (and usually they run in the background) and they have no user interface.  You have to be careful with Service, however, because they do not automagically run on their own thread; you have to spin up a thread for them.  In any case, once again, services exist more or less independently of any given application.

Context and the Manifest

This is all mitigated somewhat by two factors: Contexts and The Manifest.  Contexts are the base class of Activity, and their job is to access Android services and preference, create views and access resources.  They are, according to the Xamarin documentation, “the closed Android gets to a reference to the current application.” 

Contexts also tell the application what permissions it has, how to create controls, how to access preferences and a host of related information. But, and here’s the rub, there is no single application Context.  Instead, as an Activity creates a new Activity it passes its context along.  It is more of a conspiracy than it is an application!

Finally, the Manifest file does to a degree bring the Activities, Intents and Services together into an application.  The Manifest is an xml file that contains application information such as which components comprise the app (including registration of Activities and Intents) as well as permissions and, important in the Android world, OS version compatibility.

If you internalize all of the above, and then understand that application life-cycle (covered in detail in a future blog entry) then you are more than half way home.

An Application

Let’s put all this together with yet another simple application.  This one is based on the sample application HelloMultiscreen in the Xamarin tutorials.

This time start up your new application in Visual Studio.  Choose Android under templates, and Android Application.  Name your application TwoActivities, as shown in the figure.

NewProject

Visual Studio will helpfully create Activity1.cs for you.  Rename the class (and the file) to FirstActivity. What is particularly nice is that all your Visual Studio tools and plugins (such as Resharper) continue to work,

Rename 

Remove the count member variable and all the content in OnCreate except the call to the base method.

Notice the Activity Attribute that VS added to the Activity.  The information provided by this attribute is used in the creation and population of the AndroidManifest.xml file.  When the program is compiled the attribute data is collected form all the class files in the project and a new AndroidManifest.xml file is created based on the one already in the project but with the additional information provided by the attributes.

Our UI for this page is in Resources/ Layout/ Main.axml.  Double clicking on that file shows that there is a button already there. Examining the Source pane shows that the Button is inside a LinearLayout.  Let’s modify the button in design view by clicking on it and modifying its properties.  Set its id to @+id/ShowSecondActivity and set its text to Show Second Activity. 

Implementing The First Activity

Our UI looks world class, but we have to make sure that it will be created by FirstActivity.  Return to that class and add the line

SetContentView(Resource.Layout.Main);

You should get Intellisense help if you saved the Main.axml file,

Intellisense

You now need to get a reference to the button and use that reference to create an event handler for the click event,

var showSecondActivity = FindViewById<Button>(

    Resource.Id.ShowSecondActivity);

showSecondActivity.Click += (sender, e) =>

{

    StartActivity(typeof (SecondActivity));

};

Notice that the button is not available as a member just because you defined it in the resource; you must first get a reference to it by calling FindViewById.  Buttons, like all controls, are of type View.

Notice that we just call StartActivity.  Under the hood, Xamarin is doing a bit more work for you, first creating an intent, passing in the current Activity as the context.

You’ll also notice that SecondActivity has a red squiggly line because we’ve not yet define it.  Let’s do that now.

Second Activity

Create your second activity by right clicking on the project and choosing Visual C# in the left pane and Activity in the right.  Name it SecondActivity.cs.  The second activity will display a TextView inside a layout.  To create the UI right click on the Layout folder and choose Add –> New Item –> Android Layout.  Name it Second.axml. 

Make sure you are in Design view and drag a TextView onto the new Layout.  Use the properties window to set the id to @+id/screen2Label and the text to “This is the second activity!”

Loading the Second Activity

Once again, you have to make sure that the activity loads its UI, so return to SecondActivity.cs and update OnCreate so that it looks like this:

protected override void OnCreate(Bundle bundle)

{

    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Second);

}

Running the App

That’s it, we’re ready to go.  If you are using GenyMotion (and you should be, see this blog post), fire up your emulator and unlock the screen.  Next, start the application and pick the emulator. You should see your first Activity. Click the button and you should see your second Activity.

Passing Data Between Activities

That worked great, but what if there was data you wanted to send from the first Activity to the second.  In that case, you can use the PutExtra method of the intent, but you’ll need to explicitly create the intent to do so.

Return to the code for the click event handler in the First Activity and modify it as follows,

showSecondActivity.Click += (sender, e) =>

{

    var second = new Intent(this, typeof (SecondActivity));

    second.PutExtra("DataFromFirstActivity", "This is data from the first Activity");

    StartActivity(second);

    // StartActivity(typeof (SecondActivity));

};

Notice that we are now creating the Intent explicitly and then callinig PutExtra on that intent, passing in a key and a value (in this case, both strings).  We can then call StartActivity, passing in the intent.

Over in SecondActivity we’ll have the TextViw display the data that was tucked away in the intent.  To do so, we call the corresponding GetStringExtra method, passing in the key and retrieving the value (or returning “Data not available” if the value is null),

protected override void OnCreate(Bundle bundle)

{

    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Second);

    var label = FindViewById<TextView>(Resource.Id.screen2Label);

    label.Text = Intent.GetStringExtra("DataFromFirstActivity") ??

        "Data not available!";

}

If all goes well, your SecondActivity will display the data stored in FirstActivity, as shown in the first image at the top of this blog post.

Widgets, Toolboxes and Property Windows

You’ll notice that this time we dragged widgets onto the design surface and modified their properties using the Property Window.  This is a new development in Xamarin and one which makes developing the UI infinitely easier.  Prior to this, you had to create the entire UI by hand in XML, and while I do a fair amount of XAML coding by hand, having the design surface in Xamarin is a great time saver.

The End of the Beginning

We have covered creating a simple application twice now, and we have our emulator in place.  We’ve looked at the basic building blocks, though there is much more to say.  We have, in fact, covered the beginning of Xamarin programming and it is time to dive a bit deeper in future blog posts. I look forward to it.

About the Author

JesseJesse Liberty is a Master Consultant for Falafel Software, an author and he creates courses for Pluralsight.    Liberty  hosts the popular Yet Another Podcast and his blog is required reading. He was a Senior Evangelist for Microsoft,  a XAML Evangelist for Telerik, a Microsoft MVP, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank. Jesse can be followed on twitter at @JesseLiberty

The following two tabs change content below.