My Introduction To Android Activities

One good way to get started with Android is to find others also trying to learn. Here in the Triangle area of North Carolina, there is a Meetup group for Android developers. It is called Tridroid. It’s been a great source of Android tips and a good way to meet other developers.

There was a very good Tridroid meeting earlier this month. Luke Meyer walked us through a tutorial about the Android activity lifecycle. I picked up many really useful tips about getting starting in the Android development world. It’s the sort of thing you don’t get except by watching someone else type and think through whatever comes up on the screen.

I now have a basic understanding of Activity objects. That includes:
– how activities and views connect;
– what the activity lifecycle is;
– what states an activity can be in;
– how one activity can start another.

I took what we worked on in the group and fleshed it out a bit. I ended up with something that looks like Figure 1 and Figure 2.

Figures 1-2:

There’s a text box and two buttons. The first button starts a new activity that runs and takes the full screen. The second button starts a new activity that displays as a dialog box. The second activity is shown in Figure 2

Looking at the manifest file, you will see that there are three different activity definitions. Two are ordinary: Main and Sub1. DialogActivity is different. Note the theme attribute value “Theme.Dialog”. That says that it should start as a dialog activity.

<activity android:name=".Main" android:label="@string/app_name">            
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />            
 </intent-filter>        
</activity>        
<activity android:name=".Sub1" android:label="Sub1 Activity"></activity>        
<activity android:name=".DialogActivity" android:label="Read This"
              android:theme="@android:style/Theme.Dialog">        
</activity>

As shown in Figure 2, the DialogActivity overlays the previous activity.

The next figure shows what the Main onCreate method looks like. After setting its content view, it locates the two buttons with findViewById and defines actions for the button clicks.  The two actions start activities in exactly the same way.  You see that it does not take much to start an activity, just a call to startActivity with the Activity class you want to run. As mentioned above, it is the theme attribute that makes the second one run as a dialog.

Figure 3: onCreate method of Main (click for larger image)

If you are new to Android, you will want to spend a bit of time understanding the call “setContentView (R.layout.main)”. There is quite a bit of Android magic there. Basically what it is saying is set the main view for this activity to a view defined by the main.xml file in the resources layout folder (/res/layout). You can define view objects in java code or in xml files. My sense so far is that most people find it more convenient to define the views in xml files. If you look in the resources folder of this app, you see three layout files. The main.xml file lays out the view with an EditText and two Button views.

Note the last line in the onCreate method. It is a call to a trace function. That is the thing that makes the activity lifecycle come alive. In each of the activity’s On methods (onCreate, onStart, onResume, etc.) there is a call to a trace method. The method makes an entry in a debugging log and also throws something up on the screen so you can see the transition.

Figure 4: Toast message

Those little messages help a lot. You can see what happens when you start another activity. You can see what happens when you stop an app. And for a little surprise, see what happens when you change the orientation of the device — the activity gets completely shut down and restarted. Knowing that should save a lot of time later in your real app. (By the way, to try that in the emulator, use CTRL-F12 and CTRL-F11 keys.)

The trace methods look like this:

public void toast (String msg) {  Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show (); }
private void trace (String msg)
{
 Log.d("Demo", msg);
 toast (msg);
}

The Log.d call puts things into the Logcat part of the DDMS window. The toast call displays a short-lived message on the device screen. Both are those help a lot in debugging. If like me, you still use System.out.println, you should know that these messages end up in the Logcat area too.

Figure 5: Logcat within DDMS (click to see larger image)

Source Code

Source code is available in Google Docs. It is a zip of an Eclipse project. It was tested with version 2.2 of Android in the Android Emulator. My Eclipse version is 3.5.2.

If you find that the app does not build in Eclipse, be sure to do a clean build by using the “Clean” item on the Project menu.

References and Acknowledgements

If you have not already done so, read the Android Fundamentals article.

Thanks to Luke Meyer for a great code-and-tell session. Check out his blog at http://sosiouxme.wordpress.com/.

About Bill Lahti

Bill Lahti is an indie game developer. Latest project: an exploration game. Recent game: Hop, Skip, and Thump, a simple abstract strategy game. Other iOS and Android games: Double Star II, Wing Leader, Gomoku League.
This entry was posted in Android and tagged , , , , . Bookmark the permalink.

1 Response to My Introduction To Android Activities

  1. Pingback: How To Build A Dashboard User Interface In Android « More Is Not Always Better

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.