Activity Lifecycles

Intro

An Android Activity is a component of an application that presents the user with a single interface with which to interact.  Typically, applications are composed of multiple Activities that are bound together to present a complete experience.  One Activity must be designated at the "main" activity, and serves as the starting point when the application is launched.  Each Activity must be explicitly declared in the applications manifest file.  Activities can be in one of many states throughout their use.  For example, say you're looking at pictures in the photo viewer app and you receive a text message.  You click on the notification in the task bar and the text messaging application is launched.  Your photo viewer activity is placed in the background,  and the text messaging app comes to the foreground.  When you go back to the photo viewer, you pick right back up where you left off.  How does that happen?  Which application resources are preserved, and which are not?  How long will the background app exist before its destroyed?  All of those questions and more can be answered by understanding the Activity Lifecycle!

Lifecycle Stages

The below state diagram shows the states of an Activity, and the methods that are invoked as the Activity transitions between states.  Notice I said 'Activity' and not 'Application'.  If you have an application with multiple activities, each one will go through this lifecycle independently: 

Within the activity lifecycle, there are multiple loops.  The main three are:

  1. Entire Lifetime: the main loop occurs from the time an activity is launched until it is shut down.  The activity may not necessarily be visible or interacted with depending on which sub-loop it is in.
  2. Visible: the loop capturing the time when the use is able to see, but not necessarily interact with, the activity.  It begins with onStart() and ends with onStop(),
  3. Foreground:  the loop capturing the time when the user is able to see and interact with the activity, beginning with onResume() and ending with onPause().  

Before going on, read the following description from Google: Activity Lifecycle

Demonstration

The Log Class

The Log class is used to output information to LogCat.  The syntax to create a LogCat entry is

Log.i(String TAG, String Message); 

You have five options when creating log entries.  Each type is color coded for easier parsing by the human eye (HINT: Red == Bad).They are (in order of most to least verbose):

From Google: 

Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.

We'll use info logs for today's lifecycle demonstration.  Because the Log method call requires a Tag string be specified, a common method is to define a Constant String variable to use for the TAG string, e.g:

final static String TAG = "PEPIN";

Let's  write an info log entry to LogCat by adding the following code to the onCreate() method:

Log.i(TAG, "Inside onCreate()");

Now run your app and watch the LogCat output in the window that opens in the bottom of the IDE.  When you app is launched on the (virtual) device, you should see the new entry (4th from bottom in the mage below):

Notice in the third column shows the TAG value we defined in out Activity.  We can use the Tag to help narrow down our search when scouring through the LogCat for clues about our running app.  As you can imagine, LogCat can get really cluttered, really fast!  In order to show only the log entries we created, we can use a filter.  For a temporary filter, simply type the TAG name in the search bar.  To create a reusable filter, selector to the right of the search bar and select 'Edit Filter Configuration' .  You will be presented with the dialog box to create a new filter.  Click on the + icon and create a filter for our TAG value: 

You can name the filter whatever you want.  We want to filter by Log Tag.  Specifically, we want to filter by the TAG we defined in our Activity...so put that value in the by Log Tag field and click OK.

 

Back To LifeCycles

In MainActivity, add the remaining methods that correspond with the methods in the state diagram above:

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

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

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

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

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

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