Developing Android Applications Part 5: Activities

The Activity class within the Android platform is something you need to get completely to grips with before you proceed: In true Hitchhikers Guide fashion, allow me to relay to you what the The official javadoc provided with the android SDK has to say about the Activity class;

“An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup).”

A single focussed thing huh? When I first started Android development this was a tough concept to get to grips with, before I realised that in all of the time I’d spent using OO techniques I’d forgotten the main reason we use it in the first place: Because it allows us to model real world entities within the scope of a development environment. Years of exposure to web development, where objects are sterilized into either in-memory representations of database entities or response/request attributes had almost blinded me to this. I was looking for a way to think of an Activity purely on the merits of the members and methods it implemented. This did not help!

So it was time to go a bit Zen… ponder on purely the concept for a second: “An Activity is a single focussed thing that a user can do”. Don’t try and dissect it into any kind of computer concept, just think of it as a real world concept. They could be looking at a map, filling out a form, playing a game or browsing a gallery: These are all activities. Your application should be designed around the idea that a user will engage in various activities while using it, and the SDK is helping you to separate these out.

Just as the javadoc says, activities will have a View associated with them, and this view can take up either all of the screen or only part of the screen. If you’re an asp.net web forms developer, think of activities as your “code behind” files. The only difference is that here, rather than you having to bastardize the DOM in order to achieve what is roughly semantically correct, you actually have a real, working semantic model for representing a user interaction. If you’re an MVC developer, these can also be thought of as your controllers. Although I’m trying to draw parallels here, please note that these are approximations and should not be taken as literal comparisons!

All activities, as well as having classes, must be registered within your application manifest (AndroidManifest.xml). You do this by simply adding the following code to the manifest, within the application tag;

<activity     android:name=".MyActivity"
android:label="@string/app_name">
</activity>

That is, assuming that you’ve got an accompanying “MyActivity” class and you’ve left the “app_name” node as per the default within your strings.xml file. If you’re using eclipse, opening the ApplicationManifest.xml file will probably default you to the UI version of the editor. Personally, I believe these sorts of contraption to be the work of the devil 🙂 To get rid of it, all you have to do is use the tabs at the bottom of the screen to select the AndroidManifest.xml tab. Then you’ll see the actual xml that makes up your manifest.

You’ve already seen that at least one Activity must be at the heart of every application, and you can even specify the name of this activity in the creation of a project. Game developers: Please don’t think of this as your “main” method (you can have something like that, but this isn’t it!). This concept of a “main activity” is more akin to the idea of a website “index” or “home” page. One of your activities must serve as this page (the first activity a user will engage in when they first start your application). You specify which activity this is using the Application manifest again, this time with a child node within the activity node itself;

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
</intent-filter>

 


 

How can an activity “hand off” to another activity?

Okay: Start a new android project and replace your layout.xml file’s content with the following;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I'm right here!!!!!"
/>

<Button
android:id="@+id/theButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="COME ON!!! DO IT!!!"
android:onClick="buttonClicked" />
</LinearLayout>

If you run your application, you should see a screen like this;

Clicking the button will result in an application error. This is because we haven’t implemented the listener yet (something to actually handle the button click, even though we have already defined this in our XML UI).

We’ll do this now. Create a second activity (remembering that you also have to declare this in the application manifest). If you use the following for the actual class;

public class TheOtherOne extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("It it BLEEEDS, WE CAN KILL ITTTTT!!!!");
setContentView(tv);
}
}

Then you’ll be needing this in your application manifest (because remember, we need to register all activities in there);

<activity android:name=".TheOtherOne"
android:label="@string/app_name">
</activity>

Okay: So now the last thing is to make the hand-off then! Go back to whatever your ‘index’ activity is and replace the contents of the oncreate method with the following;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button theButton = (Button) findViewById(R.id.theButton);

theButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), TheOtherOne.class);
startActivityForResult(myIntent, 0);
}
});
}

And there you go. Clicking the button on the resulting application should result in the following being displayed;

And that’s basically how you get your user to navigate around the various activities that your application wants to engage them in. Later on in this series, we’ll come to the concept of an “Adapter”, and we will definitely do more on the “Intent” object (very, very important!) but for now the listener approach described above should be familiar enough to web developers (it’s an event listener guys!) to help you getting along with your own Android learning. If you want to do more reading in the meantime, both the adapter and intent are good starting points. If you don’t fancy either of those, look into the concept of a “service”, one of the cooler aspects of the Android platform and one that provides a major advantage over its rivals.

Advertisements

3 thoughts on “Developing Android Applications Part 5: Activities

  1. “Don’t try and dissect it into any kind of computer concept, just think of it as a real world concept. They could be looking at a map, filling out a form, playing a game or browsing a gallery: These are all activities. Your application should be designed around the idea that a user will engage in various activities while using it, and the SDK is helping you to separate these out.”
    Where else can I read about this?

    • Well, the very best place to get all the info you’d require would be at the following address;

      http://developer.android.com/reference/android/app/Activity.html

      The point i was trying to make is that most programmers have a tendency to forget that with objects, you’re trying to represent a real-world object. When you see so many objects every day, after a while they can tend to just become sets of methods and members and it’s easy to forget their purpose.

      The Activity, i found, was one such instance where you just have to take a step back and take it as a concept before you can understand it computationally. I’d be happy to answer any specific questions you have though man.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s