Activity Life Cycle 1


One aspect that differentiates a good developer from an okay one is their understanding of what goes on behind the scene. For the sake of simplicity, the Android operating system does a lot of micromanagement that is not visible to users and in some cases developers too. A step to understanding what happens behind the scene is to familiarize yourself with the activity life cycle.

Here is a graphical representation of the Activity life cycle

activity life cycle

Activity State

An activity can be in one of several states:

  • starting: in process of loading up, but not fully loaded
  • running: done loading and now visible on the screen
  • paused: partially obscured or out of focus, but not shut down
  • stopped: no longer active, but still in device’s active memory
  • destroyed: shut down and no longer loaded in memory

Transitions between these states are call events. You can listen to these events in your activity code with the methods:

  • onCreate
  • onPause
  • onResume
  • onStop
  • onDestory

onCreate method

This method is the “constructor” of the activity. In onCreate, you create and set up the activity object, load any static resources such as images, layouts, set up menus and etc. After this method, the Activity object exists.

public class SampleActivity extends Activity
{
    ...
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstatnceState);        // always call super
        setContentView(R.layout.activity_sample);   // set up layout
        // anything else you need at initialization
    }
}

onPause method

When onPause gets call, your activity is partially visible. The activity may be in this state temporary or on its way to termination.

In this state some other operations occur such as:

  • stop animations or other actions that consumes CPU
  • commit unsaved changes (e.g. draft email)
  • release system resources that affects battery life
public void onPause()
{
    super.onPause();              // always call super
    if (my_connection != null)
    {
        my_connection.close();    // release resources
        my_connection = null;
    }
}

onResume method

When onResume gets call, your activity is coming out of the Paused state and transitioning to Running state again. This method also gets call when the activity is first created/loaded. On the initial call to onResume the following occurs:

  • initialize resources that you will release in onPause
  • start/resume animations or other ongoing actions that should run only when the activity is visible on-screen
public void onResume()
{
    super.onResume();    // always call super
    if (my_connection == null)
    {
        my_connection = new SampleConnection();    // init resources
        my_connection.connect();
    }
}

onStop method

When onStop gets call, your activity is no longer visible on the screen. There are multiple causes to this, the following are some:

  • user chose another app from Recent Apps window
  • user starts a different activity in the app
  • user receives a phone call while the app is running
  • etc

In this state, your app might still be running, just that activity is not. Something to remember is onPause always gets call before onStop. The onStop method performs heavy-duty shutdown tasks like writing to a database.

public void onStop()
{
    super.onStop();    // always call super
    // do shutdown tasks
}

onStart and onRestart method

The onStart method gets call every time the activity begins.

public void onStart()
{
    super.onStart();    // always call super
    ...
}

The onRestart method gets call when the activity was stopped but is starting again. This is not commonly use because onResume is the better of the two. When onRestart gets invoke, it reopens any resources that onStop closed.

public void onRestart()
{
    super.onRestart();    // always call super
    ...
}

onDestroy method

When onDestroy gets call, your entire app gets shut down and unloaded from active memory. The system calls onDestroy when it wants to reclaim the memory used by your app. This make it unpredictable exactly when/if onDestroy will get call. It is better to favor onStop or onPause because they get invoke in a predictable and timely manner.

public void onDestroy()
{
    super.onDestroy();    // always call super
    ...
}

How to Check Activity State Transitions

To check if events are getting called, you can use the LogCat system for logging messages when your app changes states. It is the equivalent of “print” statement debugging for Android apps. The messages get display in the LogCat console in Android Studio.

public void onStart()
{
    super.onResume();    // always call super
    Log.v("testing", "onStart was called\n");
    // set up code here
}

Another way to check is to create a simple scroll text view and write your debug messages to the text view.

Log methods

Here are some common Log methods:
log_methods_activity

I hope I have provided some valuable insight to you all. If you found this post helpful, share it with others who may benefit from this. Also, follow me on twitter for more content. Until next post, take care and happy coding.


About Steven To

Steven To is a software developer that specializes in mobile development with a background in computer engineering. Beyond his passion for software development, he also has an interest in Virtual Reality, Augmented Reality, Artificial Intelligence, Personal Development, and Personal Finance. If he is not writing software, then he is out learning something new.

One thought on “Activity Life Cycle

Comments are closed.