Android App Development with Java: All About Android Activities - dummies

Android App Development with Java: All About Android Activities

By Barry A. Burd

If you look in the app/manifests branch in Android Studio’s Project tool window, you see an AndroidManifest.xml file. The file isn’t written in Java; it’s written in XML.

Here is some code from an AndroidManifest.xml file. With minor tweaks, this same code could accompany lots of examples.

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

Here’s what the code “says” to your Android device:

  • The code’s action element indicates that the activity that’s set forth (the MainActivity class) is MAIN.

Being MAIN means that the program is the starting point of an app’s execution. When a user launches the app, the Android device reaches inside the code and executes the code’s onCreate method. In addition, the device executes several other methods.

  • The code’s category element adds an icon to the device’s Application Launcher screen.

On most Android devices, the user sees the Home screen. Then, by touching one element or another on the Home screen, the user gets to see the Launcher screen, which contains several apps’ icons. By scrolling this screen, the user can find an appropriate app’s icon. When the user taps the icon, the app starts running.

The category element’s LAUNCHER value makes an icon for running the MainActivity class available on the device’s Launcher screen.

So there you have it. With the proper secret sauce (namely, the action and category elements in the AndroidManifest.xml file), an Android activity’s onCreate method becomes an app’s starting point of execution.

Extending a class

Often, the words extends and @Override tell an important story — a story that applies to all Java programs, not only to Android apps.

Many examples contain the lines

import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

When you extend the android.support.v7.app.AppCompatActivity class, you create a new kind of Android activity. The words extends AppCompatActivity tells Java that a MainActivity is, in fact, an example of an Android AppCompatActivity. That’s good because an AppCompatActivity is a certain kind of Android activity. The folks at Google have already written thousands of lines of Java code to describe what an Android AppCompatActivity can do. Being an example of an AppCompatActivity in Android means that you can take advantage of all the AppCompatActivity class’s prewritten code.

When you extend an existing Java class (such as the AppCompatActivity class), you create a new class with the existing class’s functionality.

Overriding methods

Often, a MainActivity is a kind of Android AppCompatActivity. So a MainActivity is automatically a screenful of components with lots and lots of handy, prewritten code.

Of course, in some apps, you might not want all that prewritten code. After all, being a Republican or a Democrat doesn’t mean believing everything in your party’s platform. You can start by borrowing most of the platform’s principles but then pick and choose among the remaining principles. In the same way, the code declares itself to be an Android AppCompatActivity, but then overrides one of the AppCompatActivity class’s existing methods.

If you bothered to look at the code for Android’s built-in AppCompatActivity class, you’d see the declaration of an onCreate method. The word @Override indicates that the listing’s MainActivity doesn’t use the AppCompatActivity class’s prewritten onCreate method. Instead, the MainActivity contains a declaration for its own onCreate method.

In particular, the onCreate method calls setContentView(R.layout.activity_main), which displays the material described in the res/layout/activity_main.xml file. The AppCompatActivity class’s built-in onCreate method doesn’t do those things.

An activity’s workhorse methods

Every Android activity has a lifecycle — a set of stages that the activity undergoes from birth to death to rebirth, and so on. In particular, when your Android device launches an activity, the device calls the activity’s onCreate method. The device also calls the activity’s onStart and onResume methods.

You can declare your own onCreate method without declaring your own onStart and onResume methods. Rather than override the onStart and onResume methods, you can silently use the AppCompatActivity class’s prewritten onStart and onResume methods.

When an Android device ends an activity’s run, the device calls three additional methods: the activity’s onPause, onStop, and onDestroy methods. So, one complete sweep of your activity, from birth to death, involves the run of at least six methods: onCreate, then onStart, and then onResume, and later onPause, and then onStop, and, finally, onDestroy. As it is with all life forms, “ashes to ashes, dust to dust.”

Don’t despair. For an Android activity, reincarnation is a common phenomenon. For example, if you’re running several apps at a time, the device might run low on memory. In this case, Android can kill some running activities. As the device’s user, you have no idea that any activities have been destroyed. When you navigate back to a killed activity, Android re-creates the activity for you and you’re none the wiser. A call to super.onCreate(savedInstanceState) helps bring things back to the way they were before Android destroyed the activity.

Here’s another surprising fact. When you turn a phone from Portrait mode to Landscape mode, the phone destroys the current activity (the activity that’s in Portrait mode) and re-creates that same activity in Landscape mode. The phone calls all six of the activity’s lifecycle methods (onPause, onStop, and so on) in order to turn the activity’s display sideways.

It’s similar to starting on the transporter deck of the Enterprise and being a different person after being beamed down to the planet (except that you act like yourself and think like yourself, so no one knows that you’re a completely different person).