In this article, I will explain about activity lifecycle and states of an Android Application. To create an environment for Android application development, refer to the below article.
An activity represents presentation layer in Android (UI). In other words, Activities are a page in your application i.e. a screen which a user sees. It can have several activities which can be switched in between during runtime of the application.
In an application, there are multiple activities that are loosely bound to each other. It works on the LIFO method. It means each time when a new activity starts, the previous activity is stopped, but the system preserves it in a stack or back stack. It is very important to manage transition between various activity states.
The following are the various life cycle methods of an Android system,
- onCreate():
This method is called when an activity is created. It is used to initialize the activity. In this step, you can do all normal static set like create views, data binding with lists etc.
- onStart(): This is called when the activity is visible to the user.
- onResume(): This method is called just before the activity starts interacting with the user.
- onPause():
When any activity is paused, it doesn’t receive any user input and execute any code and called when the current activity is being paused and previous activity is being resumed.
- onStop(): This method is called once the activity is no longer visible or activity is supposed to destroy.
- onDestroy(): This is called before the activity is destroyed by the system.
- onRestart(): This is called when the activity restarts after stopping it.
Above diagram shows complete activity lifecycle of an android application. It works by LIFO system and thus a new activity is started, it is placed on the top of the stack and the previous activity always remains below in the stack.
The following are the various states of an activity.
- Running - In this state, activity is visible and interacts with the user.
- Paused – Activity is partially visible but might be killed by the system.
- Stopped – Activity is not visible, instance is running but might be killed by the system.
- Killed – Finally, activity has been terminated by calling finish() method.
Example of Android activity lifecycle.
- package com.example.ppandey.myapplication;
-
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d("Android Lifecycle", "onCreate Invoked");
- }
- public void onStart()
- {
- super.onStart();
- Log.d("Android Lifecycle","onStart Invoked");
- }
- public void onResume()
- {
- super.onResume();
- Log.d("Android lifecycle","onResume Invoked" );
- }
- public void onPause()
- {
- super.onPause();
- Log.d("Android Lifecycle","onPause Invoked");
- }
- public void onStop()
- {
- super.onStop();
- Log.d("Android lifecycle","onStop Invoked");
- }
- public void onRestart()
- {
- super.onRestart();
- Log.d("Android lifecycle","onRestart Invoked");
- }
- public void onDestroy()
- {
- super.onDestroy();
- Log.d("Android lifecycle","onDestroy Invoked");
- }
-
- }
When the application is running, first 3 methods onCreate(), onStart() and onResume() are executed one by one,
When back button is clicked, onPause() and onStop methods are invoked,
When the app is reopened, onRestart(), onStart() and onReume() methods are invoked.
Read more articles on Android: