Activity Lifecycle in Android with Demo App

In Android, an activity is referred to one screen in an application. It is very similar to a single-window of any desktop application. An Android app consists of one or more screens or activities.

Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when a new activity starts, the previous one always remains below it. There are four stages of an activity.

  1. If an activity is in the foreground of the screen i.e at the top of the stack, then it is said to be active or running. This is usually the activity that the user is currently interacting with.
  2. If an activity has lost focus and a non-full-sized or transparent activity has focused on top of your activity. In such a case either another activity has a higher position in multi-window mode or the activity itself is not focusable in current window mode. Such activity is completely alive..
  3. If an activity is completely hidden by another activity, it is stopped or hidden. It still retains all the information, and as its window is hidden thus it will often be killed by the system when memory is needed elsewhere.
  4. The system can destroy the activity from memory by either asking it to finish or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

For each stage, android provides us with a set of 7 methods which have their own significance for each stage in the life cycle. The image shows a path of migration whenever an app switches from one state to another.

Detailed introduction on each of the method is stated as follows:



  1. onCreate():It is called when the activity is first created. This is where all the static work is done like creating views, binding data to lists, etc. This method also provides a Bundle containing its previous frozen state, if there was one.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            // Bundle containing previous frozen state
      
            setContentView(R.layout.activity_main);
            // The content view pointing to the id of layout
            // in the file activity_main.xml
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

  2. onStart():It is invoked when the activity is visible to the user. It is followed by onResume() if activity is invoked from background. It is also invoked after onCreate() when activity is first started.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            // Bundle containing previous frozen state
      
            setContentView(R.layout.activity_main);
            // The content view pointing to the id of layout
            // in the file activity_main.xml
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
      
        protected void onStart()
        {
      
            // It will show a message on the screen
            // then onStart is invoked
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onStart Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

  3. onRestart():It is invoked after the activity has been stopped and prior to its starting stage and thus is always followed by onStart() when any activity is revived from background to on screen.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            // Bundle containing previous frozen state
      
            setContentView(R.layout.activity_main);
            // The content view pointing to the id of layout
            // in the file activity_main.xml
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
      
        protected void onRestart()
        {
      
            // It will show a message on the screen
            // then onRestart is invoked
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onRestart Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

  4. onResume():It is invoked when the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, with a user interacting with it. Always followed by onPause() when the activity goes into the background or is closed by the user.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            // Bundle containing previous frozen state
            super.onCreate(savedInstanceState);
      
            // The content view pointing to the id of layout
            // in the file activity_main.xml
            setContentView(R.layout.activity_main);
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
      
        protected void onResume()
        {
      
            // It will show a message on the screen
            // then onResume is invoked
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onResume Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

  5. onPause(): It is invoked when an activity is going into the background but has not yet been killed. It is a counterpart to onResume(). When an activity is launched in front of another activity, this callback will be invoked on the top activity (currently on screen). The activity, under the active activity, will not be created until the active activity’s onPause() returns, so it is recommended that heavy processing should not be done in this part.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
      
            // Bundle containing previous frozen state
            super.onCreate(savedInstanceState);
      
            // The content view pointing to the id of layout
            // in the file activity_main.xml
            setContentView(R.layout.activity_main);
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
      
        protected void onPause()
        {
      
            // It will show a message on the screen
            // then onPause is invoked
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onPause Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

  6. onStop(): It is invoked when the activity is not visible to the user. It is followed by onRestart() when the activity is revoked from the background, followed by onDestroy() when the activity is closed or finished, and nothing when the activity remains on the background only. Note that this method may never be called, in low memory situations where the system does not have enough memory to keep the activity’s process running after its onPause() method is called.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
      
            // Bundle containing previous frozen state
            super.onCreate(savedInstanceState);
      
            // The content view pointing to the id of layout
            // in the file activity_main.xml
            setContentView(R.layout.activity_main);
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
      
        protected void onStop()
        {
      
            // It will show a message on the screen
            // then onStop is invoked
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onStop Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

  7. onDestroy():
    The final call received before the activity is destroyed. This can happen either because the activity is finishing (when finish() is invoked) or because the system is temporarily destroying this instance of the activity to save space. To distinguish between these scenarios, check it with isFinishing() method.Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.piyush.myapplication;
      
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
      
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            // Bundle containing previous frozen state
            super.onCreate(savedInstanceState);
      
            // The content view pointing to the id of layout
            // in the file activity_main.xml
            setContentView(R.layout.activity_main);
      
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onCreate Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
        protected void onDestroy()
        {
      
            // It will show a message on the screen
            // then onDestroy is invoked
            Toast toast
                = Toast.makeText(
                           getApplicationContext(),
                           "onDestroy Called",
                           Toast.LENGTH_LONG)
                      .show();
        }
    }

    chevron_right

    
    

Demo Android app to demonstrate Activity Lifecycle in Android:

filter_none

edit
close

play_arrow

link
brightness_4
code

package com.example.piyush.myapplication;
  
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onCreate Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
  
    protected void onStart()
    {
        super.onStart();
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onStart Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
  
    @Override
    protected void onRestart()
    {
        super.onRestart();
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onRestart Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
  
    protected void onPause()
    {
        super.onPause();
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onPause Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
  
    protected void onResume()
    {
        super.onResume();
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onResume Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
  
    protected void onStop()
    {
        super.onStop();
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onStop Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
  
    protected void onDestroy()
    {
        super.onDestroy();
        Toast toast
            = Toast.makeText(
                       getApplicationContext(),
                       "onDestroy Called",
                       Toast.LENGTH_LONG)
                  .show();
    }
}

chevron_right


Output:

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.