Android Basics — App Components and Activity Lifecycle
In this post, I will talk about the most basic part of Android development. Its components and the Activity lifecycle.
App Components

Activities:
A single screen with UI. An android app may have multiple activities (e.g. an email app might have one activity to list all the emails, another to show the
contents of each email, and another to compose a new email).
Services:
A service runs in the background. There are two types of services:
- Foreground: Service that runs in the background but informing the user about it.
- Background: Service that runs in the background and doesn’t necessarily inform the user about it.
Examples:
- a music player would be running in the background even after the application is closed, but informs the user about the process through a notification. (foreground)
- a service that downloads data from the internet in the background (background)
Content Providers:
It manages shared app data. There are four ways to store data in an
app: it can be written to a file and stored in the file system, inserted or updated to an SQLite database, posted to the web, or saved in any other persistent storage location the App can access. Through content providers, other apps can query or even modify the data stored by an app.
Broadcast Receivers:
It responds to the system-wide broadcast of announcements (eg: a broadcast announcing that the screen has turned off, the battery is low, etc). Broadcast receivers don’t have UI but they can show notifications to alert the user.
Activity Lifecycle

onCreate(Bundle savedInstanceState):
This method is called when the activity is created. This is the ideal location to perform most of the initialization tasks.
onRestart():
This method is called when the activity is about to restart after having previously stopped by the runtime system.
onStart():
This method is called immediately after the onCreate()
or onRestart()
methods. This method indicates that the activity is about to become visible to the user. This call is followed by a call to onResume()
or onStop()
depending on if the activity moves to the top of the activity stack or is pushed down the stack by another activity.
onResume():
This method is called to indicate that the activity is now at the top of the activity stack and is the activity with which the user is interacting.
onPause():
This method is called to indicate that a previous activity is about to become the foreground activity. This call will be followed by a call to either the onResume()
or onStop()
a method depending on whether the activity moves back to the foreground or becomes invisible to the user.
onStop():
This method is called when the activity is now no longer visible to the user. The two possible scenarios that may follow this call are a call to onRestart()
in the event that the activity moves to the foreground again, or onDestroy()
if the activity is being terminated.
onDestroy():
This method is called when the activity is about to get destroyed.
We will implement Activity Lifecycle in a live Android project in the next post.