Android Activity And its lifecycle

You know that every program starts with main() method if you know c, c++ or java programming. Very similar way, Android System starts an android program with in activity by calling onCreate() method. Then Android ends by calling the activity onDestroy() method. Between onCreate() and onDestroy() method, a series of methods are called.

Every time the app is opened, the activity is a point of entry for the app.

OnCreate() method is called when an activity is launched. Thus, with a first call to onCreate(), the entire life of activity begins. All of the onCreate() method configurations the globalized state. For example, the setContentView() method in this method defines the layout.

Then, onStart() method is called. It starts the visible lifetime of activity. You can see the activity on the panel, but it may not interact with the user in the foreground. We keep the resources for the activity displayed to the user during the visible lifetime. This method can be referred to several times.

Then, onResume() method is called. With call to this method, foreground lifetime of activity is started. Therefore, the user can see activity and also interact with activity.

If a new activity is in the foreground, an old activity method is called onPause(). The activity's first lifetime ends with call to onPause(). Thus, activity is not in front of all other activities and user can not interact with it now. Methods can be called onPause() and onResume() many times. For instance, when the result of an activity is delivered, a new intent, etc.

If another activity has been resumed and covers old activities, the user can't see old activities. In this situation, onStop() method is called. This ends the visible lifetime of the activity.

Now, the onDestroy() method is called if the activity is over. This call ends the entire life of activity. We are therefore releasing all the resources associated with this activity.
We show a table which shows clearly what method does in the lifecycle of activity.

Method Description
onCreate() First method to be called whenever activity is created. You should do static setup – create views, bind data to lists etc.
onRestart() This method is called when activity has been stopped.
onStart() This method is called when activity is becoming visible to the user.
onResume() This method is called when activity starts interacting with the user. At this point, activity is at top of the activity stack.
onPause() This method is called when an activity is about to start resuming a previous activity. Here, you can save changes to persistent data, stop animations, or other things that are consuming CPU.
onStop() This method is called when activity is no longer visible to the user.
onDestroy() The final call before activity is destroyed.

File: activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
< xmlns:android=""

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </>

It provides the details about the invocation of life cycle methods of activity. We show the content on the logcat in this example.


package com.ukacademe.helloworld;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG,"in method onCreate"); } @Override protected void onStart() { super.onStart(); Log.i(TAG,"in method onStart"); } @Override protected void onResume() { super.onResume(); Log.i(TAG,"in method onResume"); } @Override protected void onPause() { super.onPause(); Log.i(TAG,"in method onPause"); } @Override protected void onStop() { super.onStop(); Log.i(TAG,"in method onStop"); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG,"in method onRestart"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG,"in method onDestroy"); } }

Now run the project and see on the logcat: onCreate(), onStart() and onResume() methods.

Now click on the HOME Button. You will see onPause() method.

Now open the app again. And see on the logcat: onRestart(), onStart() methods.

Now Exit the app. And you will see onStop() and onDestroy() methods