Intents

“Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases: Starting an activity, Starting a service, Delivering a broadcast.”

Photo credit: @vogella


# Start the activity connect to the
# specified class

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
          

Android application and activity life cycle

“To manage limited system resources the Android system can terminate running applications. Each application is started in a new process with a unique ID under a unique user. If the Android system needs to free up resources it follows a simple set of rules.”

The out-of-memory killer

Foreground

Priority: 1

An application in which the user is interacting with an activity, or which has an service which is bound to such an activity. Also if a service is executing one of its lifecycle methods or a broadcast receiver which runs its onReceive() method.

Visible

Priority: 2

User is not interacting with the activity, but the activity is still (partially) visible or the application has a service which is used by a inactive but visible activity.

Service

Priority: 3

Application with a running service which does not qualify for 1 or 2.

Background

Priority: 4

Application with only stopped activities and without a service or executing receiver. Android keeps them in a least recent used (LRU) list and if requires terminates the one which was least used.

Empty

Priority: 5

Application without any active components.

States of an activity

Running

Activity is visible and interacts with the user.

Paused

Activity is still visible but partially obscured, instance is running but might be killed by the system.

Stopped

Activity is not visible, instance is running but might be killed by the system.

Killed

Activity has been terminated by the system of by a call to its finish() method.

The live cycle methods

onCreate()

Called then the activity is created. Used to initialize the activity, for example create the user interface.

onResume()

Called if the activity get visible again and the user starts interacting with the activity again. Used to initialize fields, register listeners, bind to services, etc.

onPause()

Called once another activity gets into the foreground. Always called before the activity is not visible anymore. Used to release resources or save application data. For example you unregister listeners, intent receivers, unbind from services or remove system service listeners.

onStop()

Called once the activity is no longer visible. Time or CPU intensive shut-down operations, such as writing information to a database should be down in the onStop() method. This method is guaranteed to be called as of API 11.

Photo credit: The Activity Lifecycle