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:
onCreate(android.os.Bundle arg) :: is called when an Activity is first created. When this method is invoked, an instance of the Android framework class object android.os.Bundle is passed. The Bundle object encapsulates state of the Activity
onStart() :: is called just before the Activity becomes visible to the user
onResume() :: is called when the user starts interaction with the Activity
onPause() :: is called when ever the Activity leaves the foreground. In other words, this callback method will be invoked when the HOME button of the Android device is pressed, or BACK button of the Android device is pressed, or any other Android application such as the PHONE application comes to the foreground
onStop() :: is called when the Activity is no longer visible to the user
onRestart() :: is called when the previously stopped Activity comes to the foreground
onDestroy() :: is called before the Activity is destroyed by the Android system
The following diagram depicts the lifecycle of an Activity:
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:
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:
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:
Next, modify the contents of the strings.xml file to look like the one shown in the listing 3.2 below:
Next, modify the contents of the activity_lifecycle.xml layout file to look like the one shown in the listing 3.3 below:
Finally, modify the java source file LifecycleActivity.java to look like the one shown in the listing 3.4 below:
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:
From the LogCat console we can see the log output as shown in the following figure 3.5 below:
Now, click on the HOME button on the virtual device as shown in the following figure 3.6 below:
From the LogCat console we can see the log output as shown in the following figure 3.7 below:
Now, click on the APP LIST button on the virtual device as shown in the following figure 3.8 below:
Click on the DroidActivityLifecycle application as shown in the following figure 3.9 below:
From the LogCat console we can see the log output as shown in the following figure 3.10 below:
Finally, click on the BACK button as shown in the following figure 3.11 below:
From the LogCat console we can see the log output as shown in the following figure 3.12 below:
References