Lesson Weekend

Managing Activities

As you know, Android applications are made up of a series of activities. When an app is running, Android manages these multiple activities using something called an activity stack.

When a the user navigates from one activity to another, a new activity is started. When that activity takes the foreground on the device screen, it becomes the current or “running” activity, and is placed on the top of the activity stack.

In addition to running, there are three other states an activity can be in: Stopped, restarted, and paused. Other activities (from both the same application and others) are then paused or stopped, and take lower precedence in the activity stack.

For instance, if a user is interacting with an activity, a second activity is partially visible, and two activities are stopped in the background, your activity stack would look like this:

android-activity-stack

Activity States

But why do we care what state our activity is in? Well, Android manages Activities based on their state. This helps Android identify activities that are no longer in use, allowing the OS to reclaim necessary memory and resources.

For instance, let's say the activity stack on your device looks as depicted in the diagram above. Then, suddenly, you received a phone call. The Android system would likely need to free up some memory in order for you to interact with this call. Your phone application that manages ingoing and outgoing calls would also become the top activity on the stack, and the other application would enter a stopped state. Android may also need to kill stopped activities to free up memory to handle the phone call.

Your activity stack would likely be updated to look something like this:

updated-android-activity-stack

These states can be broken into 4 main groups as follows:

  • Active or Running: Activities are considered active or running if they are visible to the user. This also means they’re at the top of the activity stack. This is considered the highest priority activity in Android.

  • Paused: When the device goes to sleep, or an activity is partially hidden, the activity is considered paused. Paused activities are alive and maintain all state and member information. This is the second highest priority activity in Android.

  • Stopped/Backgrounded: Activities completely hidden by other activities are considered stopped or “in the background”. They still try to retain their state and member information as long as possible. They are also are considered the lowest priority state. Therefore, Android will kill activities in this state if higher priority activities require more memory and resources to operate.

  • Restarted: It is possible for an activity that is anywhere from paused to stopped in the lifecycle to be removed from memory by Android. If the user navigates back to the activity it must then be restarted, restored to its previously saved state, and displayed to the user.

Lifecycle Methods

There are seven built-in methods that manage the lifecycle of an Android Application, and assist in moving activities between the states detailed above. We can override any of these methods to help making managing states a lot easier.

onCreate()

Called when the activity is first created. This method contains all normal static setup, such as creating views, initializing variables, binding data, etc. This method is always followed by onStart(). We’ve already used this method to assist in setting up our activities.

onStart()

This method is called when the activity becomes visible to the user (and therefore active). It is always followed by onStop() (if the activity becomes hidden, as detailed in the “stopped/backgrounded” state above) or onResume() (if it remains in the foreground).

onResume()

Called when the activity begins interacting with the user. Always followed by onPause().

onRestart()

Called when a stopped activity is being started again. Always followed by onStart().

onPause()

Called when the system is about to start resuming a previous activity. There are two possible lifecycle methods that will be called after OnPause():

  • OnResume() will be called if the Activity is to be returned to the foreground.
  • OnStop() will be called if the Activity is being placed in the background.

onStop()

Called when the activity is no longer visible to the user. Either because another Activity has resumed, and is covering this one, an existing activity is coming to the foreground, or the activity is about to be destroyed. It is always followed by either onRestart() or onDestroy().

onDestroy()

Called directly before the activity is destroyed. Either because the activity is finishing (ie: finish() was called upon it), or because the system is temporarily destroying this instance of the activity to save space.

Lifecycle Loops

There are three loops Android developers are most interested in monitoring within an activity. Consider the following excerpt and diagram from the Activity Lifecycle entry in the Android Developer Guides:

android-developer-guide-diagram

The entire lifetime of an activity occurs between the first call to onCreate() through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. ...

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

Additional Information

For more information, check out the Activity Lifecycle entry from the Android Developers Guide. And the Xamarin Android Activity Lifecycle tutorial.

Terminology


  • Activity Stack: Multiple activities in different states, being managed by Android. The top of the stack is always the currently-running activity. Paused activities are next, and stopped activities below that. The stack's order may change as users interact with different activities.

Activity States

  • Active or Running: Activities are considered active or running if they are visible to the user. This also means they’re at the top of the activity stack.

  • Paused: When the device goes to sleep, or an activity is partially hidden, the activity is considered paused. Paused activities are alive and maintain all state and member information.

  • Stopped or Backgrounded: Activities completely hidden by other activities are considered stopped or “in the background”. They are considered lowest priority, and Android will kill them if additional resources and/or memory is required by the running activity.

  • Restarted: An activity that was killed by Android, but restarted by the user and returned to its previous state.

Lifecycle Methods

  • onCreate(): Called when the activity is first created. This method contains all normal static setup.

  • onStart(): Called when the activity becomes visible to the user (and therefore active).

  • onResume(): Called when the activity begins interacting with the user.

  • onRestart(): Called when a stopped activity is being started again.

  • onPause(): Called when the system is about to start resuming a previous activity.

  • onStop(): Called when the activity is no longer visible to the user.

  • onDestroy(): Called directly before the activity is destroyed.

Lifecycle Loops

  • Entire Lifetime: The entire lifetime of an activity includes every method that takes place between the first call to onCreate() through the final call to onDestroy().

  • Visible Lifetime: Referring to the time in which the activity is visible to the user, between a call to onStart() until a corresponding call to onStop()

  • Foreground Lifetime: Referring to the time in which the activity is in front of all other activities, and interacting with the user. Between a call to onResume() until a corresponding call to onPause()

Additional Information