How Android applications work :
When the user runs an application, Android starts it and brings it to the foreground. From that application, the user might invoke another application, or another screen in the same application, and then another and another. All these programs and screens are recorded on the application stack by the system’s Activity Manager. At any time, the user can press the Back button to return to the previous screen on the stack. Each user interface screen is represented by an Activity class . Each activity has its own life cycle.
In Android, an application can be “alive” even if its process has been killed , ie. the activity life cycle is not tied to the process life cycle. Processes are just disposable containers for activities
Lets take a close look
During lifetime, each activity of an Android program can be in one of several states as shown
We override these these methods in our Activity class, and Android will call them at the appropriate time
- onCreate(Bundle): This is called when the activity first starts up.
- onStart( ): This indicates the activity is about to be displayed to the user
- onResume( ): This is called when your activity can start interacting with the user. This is a good place to start animations and music.
- onPause( ): This runs when the activity is about to go into the background, usually because another activity has been launched in front of it. This is where we should save our program’s persistent
state, such as a database record being edited.
- onStop( ): This is called when our activity is no longer visible to the user and it won’t be needed for a while. If memory is tight, onStop( ) may never be called (the system may simply terminate our process)
- onRestart( ): If this method is called, it indicates our activity is being redisplayed to the user from a stopped state.
- onDestroy( ): This is called right before our activity is destroyed. If memory is tight, onDestroy( ) may never be called (the system may simply terminate your process).
- onSaveInstanceState(Bundle): Android will call this method to allow the activity to save per-instance state, such as a cursor position within a text field. Usually we won’t need to override it because the default implementation saves the state for all our user interface controls automatically.
- onRestoreInstanceState(Bundle): This is called when the activity is being reinitialized from a state previously saved by the onSave-InstanceState( ) method. The default implementation restores the state of our user interface.