Taste of Android :: Part-3


Bhaskar S 05/11/2013


Android Application Development - II

An Android Activity is the graphical window screen through which a user interacts with the application. To create an Activity one must create a class that extends the Android framework class android.app.Activity. The Android framework class android.app.Activity defines a number of callback methods that correspond to the various lifecycle aspects of an Activity. The following are the various lifecycle callback methods defined in the Android framework class android.app.Activity:

The following diagram depicts the lifecycle of an Activity:

Activity Lifecycle Image
Figure-3.1

Let us build a simple Android application to illustrate the Activity lifecycle. In order to demonstrate when each of the callback methods are invoked, we need some way to output log. In order to do so, we will use the Android framework class android.util.Log. To log a debug message, we will use the method Log.d(tag, text), where tag is a string to identify the source of the debug and text is a string message we want to log.

To view the debug messages logged from our application, we will use the LogCat log viewer which is included with the Android development tools in Eclipse.

We need to configure LogCat so we can view the log messages in Eclipse. From the Eclipse menu options, choose Windows->Preferences. This action will launch the Preferences window. From the list of options on the left, choose Android->LogCat. This will bring us to the Preferences window as shown in the following figure 3.2 below:

LogCat Preferences Image
Figure-3.2

Increase the value corresponding to the field Maximum number of logcat messages to buffer. Also, uncheck the option Automatically enable/disable scroll lock based on the scrollbar position and click on the Apply button and finally click on the Ok button as shown in the following figure 3.3 below:

LogCat Preferences Image
Figure-3.3

Let us now create a new Android application named DroidActivityLifecycle

to illustrate when each of the Activity lifecycle callback methods are invoked. We will not go step-by-step to show the various screens since we already did that in Part-2 for the Tip Calculator application.

Modify the contents of the dimens.xml file to look like the one shown in the listing 3.1 below:

Listing-3.1
<resources>

<dimen name="title_size">26sp</dimen>

</resources>

Next, modify the contents of the strings.xml file to look like the one shown in the listing 3.2 below:

Listing-3.2
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">DroidActivityLifecycle</string>
<string name="action_settings">Settings</string>
<string name="lifecycle_txt">Activity Lifecycle</string>

</resources>

Next, modify the contents of the activity_lifecycle.xml layout file to look like the one shown in the listing 3.3 below:

Listing-3.3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lifecycle_txt"
android:textColor="#cc3300"
android:textSize="@dimen/title_size"
android:textStyle="bold"
android:typeface="sans" />

</LinearLayout>

Finally, modify the java source file LifecycleActivity.java to look like the one shown in the listing 3.4 below:

Listing-3.4
package com.polarsparc.android.droidactivitylifecycle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class LifecycleActivity extends Activity {
final String tag = "LifecycleActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);

Log.d(tag, "onCreate(savedInstanceState) callback method called .....");
}

@Override
protected void onStart() {
super.onStart();

Log.d(tag, "onStart() callback method called .....");
}

@Override
protected void onResume() {
super.onResume();

Log.d(tag, "onResume() callback method called .....");
}

@Override
protected void onPause() {
super.onPause();

Log.d(tag, "onPause() callback method called .....");
}

@Override
protected void onStop() {
super.onStop();

Log.d(tag, "onStop() callback method called .....");
}

@Override
protected void onRestart() {
super.onRestart();

Log.d(tag, "onRestart() callback method called .....");
}

@Override
protected void onDestroy() {
super.onDestroy();

Log.d(tag, "onDestroy() callback method called .....");
}

}

We are now ready to test our Activity Lifecycle application on the virtual Android device we created in Part-1. We will create a Run Configuration for DroidActivityLifecycle as we did in Part-2 for DroidTipCalculator.

Once the run configuration for DroidActivityLifecycle is ready, we will Run the application and the application will come to life as shown in the following figure 3.4 below:

Lifecycle Application Image
Figure-3.4

From the LogCat console we can see the log output as shown in the following figure 3.5 below:

Lifecycle Log Image
Figure-3.5

Now, click on the HOME button on the virtual device as shown in the following figure 3.6 below:

Lifecycle Application Image
Figure-3.6

From the LogCat console we can see the log output as shown in the following figure 3.7 below:

Lifecycle Log Image
Figure-3.7

Now, click on the APP LIST button on the virtual device as shown in the following figure 3.8 below:

Lifecycle Application Image
Figure-3.8

Click on the DroidActivityLifecycle application as shown in the following figure 3.9 below:

Lifecycle Application Image
Figure-3.9

From the LogCat console we can see the log output as shown in the following figure 3.10 below:

Lifecycle Log Image
Figure-3.10

Finally, click on the BACK button as shown in the following figure 3.11 below:

Lifecycle Application Image
Figure-3.11

From the LogCat console we can see the log output as shown in the following figure 3.12 below:

Lifecycle Log Image
Figure-3.12

References

Taste of Android :: Part-1

Taste of Android :: Part-2