O'Reilly logo

Learning Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The StatusActivity Java Class

Now that we have our UI designed in XML, we are ready to switch over to Java. Remember from earlier in this chapter that Android provides two ways for building user interfaces. One is by declaring it in XML, which is what we just did, and we got as far as we could (for now). The other one is to build it programmatically in Java. We also said earlier that the best practice is to get as far as possible in XML and then switch over to Java.

Our Java class for this is StatusActivity.java, and the Eclipse New Project dialog has already created the stub for this class. The class is part of the com.marakana.yamba1 Java package, and as such is part of that directory.

Creating Your Application-Specific Object and Initialization Code

As with all main building blocks in Android, such as activities, services, broadcast receivers, and content providers, you usually start by subclassing a base class provided by the Android framework and overriding certain inherited methods. In this case, we subclass Android’s Activity class and override its onCreate() method. As you recall, activities have a certain life cycle (see Activity Life Cycle), or state machine through which they go. We as developers do not control what state the activity is in, but we do get to say what happens during a transition to a particular state. In this case, the transition we want to override is the onCreate() method that the system’s ActivityManager invokes when the activity is first created (i.e., ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required