Android Activity is one of the components of Android. It represents a single screen in your application. We can say that it performs actions on the screen.
An application can have zero or more activities. Typically, applications have one or more activities.
The main aim of Activity is to interact with the user. From the moment an activity appears on the screen till the time it is hidden.
Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the “back stack”).
Each time a new activity starts, the previous activity is stopped. However, the system preserves the activity in a stack (the “back stack”).
Android activity goes through a number of stages, known as an activity’s lifecycle.
There are 7 stages in Android Activity lifecycle. ActivityManager manages these activities and shows what state your activity is.
7 methods of Android Activity Lifecycle
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
- onRestart()
The Activity can be destroyed from onStop() or onRestart() methods, let us understand the functions of each of these methods below:
Created
In this method, activity should create its UI.
Started
In this state activity is about to start, it is completely invisible.
Resumed
In this state Activity’s UI is completely visible on the screen.
Paused
Activity is about to unload or cover because of another activity, in this state activity is completely visible on the screen.
Stopped
Activity is unloaded or another activity has fully covered the first activity, in this state activity is completely visible.
Destroyed
In this state, activity is already unloaded and about to get destroyed.
Restarted
In this state, activity is about to restart because of uncovering.
The following figure explains the Activity Life Cycle:
Declare the Activity in AndroidManifest.XML file with intent filters:
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
The <action> element shows that this is the “main” entry point to the application and The <category> element allow users to launch this activity.
The best way to understand the various stages experienced by an activity is to create a new application and implement the various events.
After creating the application, inside our MainActivity we have to implement all the methods one by one and we can see all the events very well with the help of LogCat.
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // The activity is being created. } @Override protected void onStart() { super.onStart(); // The activity is about to become visible. } @Override protected void onResume() { super.onResume(); // The activity has become visible (it is now "resumed"). } @Override protected void onPause() { super.onPause(); // Another activity is taking focus (this activity is about to be "paused"). } @Override protected void onStop() { super.onStop(); // The activity is no longer visible (it is now "stopped") } @Override protected void onDestroy() { super.onDestroy(); // The activity is about to be destroyed. } }