Activity life cycle

Let’s create first activity. 

If you notice, We have created MyActivity by inheriting from a class called AppCompatActivity. AppCompat? nevertheless it is an Activity class


import android.os.Bundle;
import android.util.Log;


public class MainActivity extends AppCompatActivity  {

    private TextView resultText;

    public void onCreate(Bundle savedInstanceState) {
        Log.w("MainActivity", "on create called");

    protected void onResume() {

        Log.w("MainActivity", "on resume called");

    protected void onPause() {
        Log.w("MainActivity", "on pause called");

    protected void onStop() {
        Log.w("MainActivity", "on stop called");

    protected void onDestroy() {
        Log.w("MainActivity", "on destroy called");


The implemented functions are part of Activity life cycle. 

onCreate is called when Application first loaded (term create) – This is the right place to set content (from layout file) and initialise variables.

onResume is called just right after the onCreate as well as when coming from background to foreground. Gives us clear hook that activity’s variables are available for use. 

onPause. Is called when activity is beginning to disappear. For eg, if user press Home button, the activity will have to go to background. Activity takes a fraction of second to reach from visible to completely invisible state. The initialisation of invisible process is actually informed to us using onPause call back.

onStop is called when Activity completely go invisible. I.e., right after the onPause call, In this case complete invisible state achieved. The activity might be running in background or may be heading towards Destroy.

onDestroy is called when Activity completely removed from background., we can say this as Activity close stage. After onStop, if onDestroy function executed, we can safely assume the activity is no more in memory. (usually we get a Activity to reach destroy state by pressing “Back” button)

onCreate called for the first time when activity freshly opened, and this function will never be called until the activity reaches the onDestroy state.

setContentView(R.layout.activity_main) – 

setContentView is a function declared inside the parent class AppCompatActivity. This function loads the layout file into activity. Since onCreate is a right place to initialise variables and view objects. We call this right after the super.onCreate of onCreate function

Leave a Reply

Your email address will not be published. Required fields are marked *