Understanding the Android Activity Life Cycle
Author: Ian Darwin --
Published? true --
Android apps do not have a "main" method; you need to learn how they get started and how they stop or get stopped.
The class android.Activity provides a number of well-defined life-cycle methods that are called when an application is started, suspended, restarted, etc., as well as a method you can call to mark an Activity as finished.
Your Android application runs in its own Unix process, so in general it cannot directly affect any other running application. The Dalvik VM interfaces with the operating system to call you when your application starts, when the user switches to another application, and so on. There is a well-defined lifecycle for Android applications.
An Android application has three states it can be in:
- Active - the app is visible to the user and is running;
- Paused - the app is partly obscured and has lost the input focus.
- Stopped - the app is completely hidden from view
Your app will be transitioned among these states by Android calling the following methods on the current Activity at the appropriate time:
void onCreate(Bundle savedInstanceState)
For an application's first Activity, onCreate() is how you know that the application has been started. This is where you normally do constructor-like work such as setting up the "main window" with setContentView(), add listeners to buttons to do work (including starting additional Activities), and so on.
This is the one method that even the simplest Android app needs.
You can see the effects of the various lifecycle methods by creating a dummy project in Eclipse and overriding all the methods with log "debug" statements.