Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Android Activity lifecycle (Code) & Debugging using LogCat (Android Part – 8)

Hello Geeks, hope you have read the previous article and are clear with the theoretical concepts of an Android Activity and its Lifecycle. Now we will create a code for the same to see what all methods are called in which cases. This code is similar to what you might have created to study lifecycle of an Applet in Java (for those who are from java background). Create a new Android Application in Eclipse IDE and paste this code.

Android Lifecycle and Debugging

As you can see in the code, that we have extended the Activity class and overwritten all the methods which we had discussed in previous tutorial. In each method, we first call the super class version of the method (using super keyword) and then we write our own statement. Here we use a special class from android.util package. This is the Log class which is used for debugging in android. We use Log.d method in this program which has the following signature.

static void d ( String tag, String message )

First parameter takes the tag (you will shortly get to know what it is) and second takes the message to be printed. Anything printed using Log.d method is shown on the LogCat. LogCat is generally visible at the bottom as shown below:

logcat

The above screenshot shows the LogCat under the text area where we code. We can use the position marked as “Extend” in above screenshot to extend the logcat upwards so that it more messages are visible without scrolling.

Moreover, we also have a Debug perspective in which tabs like Console, Logcat, Variables and program breakpoints as shown here :

debug_perspective

The default perspective is Java (extreme left). In the above screenshot, the Tag is nothing but the string visible inside “Tag” column (Lifecycle here) and Message is the string visible inside “Text” column (inside onCreate(), inside onStart() etc).

For those who are from pure java background, we can also use System.out.println() for debugging, and it will be visible in Console tab, but it is not recommended by Android Developers. Moreover, it may or may not work for some versions.

When we execute a typical android application in android virtual device, we get many messages from the system (or the virtual device). So sometimes it may become hard to locate our own messages in the LogCat. To overcome this problem, we can create custom LogCat filters so that our custom LogCat reads only specific tags or messages.

1

In the above screenshot, we need to focus only on the tags marked above, but the LogCat shows all the tags which are coming from AVD. So we can create custom LogCat filter by clicking on this icon present on left side of LogCat filter.

add_filter

Then this dialog box appears

create_filter

I have all tags with name “LifeCycle”, so I have named the tag “LifeCycle”. The message doesn’t matter so I keep it blank. Click OK to create the filter.Now we are clear with basics of debugging and will execute our application with our virtual device and notice the output in LogCat when the Lifecycle methods are called.Execute the application and select your custom filter.

As per the previous tutorial, when the application starts, the methods shown below are called:

11

When we come back to home screen (using Home button on emulator), the GUI is updated and our activity is no more visible so these methods are called:

2

The application still remains in memory, so onDestroy() is not called yet !  Now we come back to our application using Menu and these methods are called in quick succession :

3

Now we use BACK button to exit the application and these methods are called :

4

So this completes the 7 basic methods in Android Lifecycle. Comment if you have any doubt !

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy