Android Programming - Day Two

Introduction

 
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 the presentation layer in Android (UI). In other words, Activities are a page in your application i.e. a screen that a user sees. It can have several activities that 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 the transition between various activity states.
The following are the various life cycle methods of an Android system,
  1. 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 sets like create views, data binding with lists, etc.
     
  2. onStart()

    This is called when the activity is visible to the user.
     
  3. onResume()

    This method is called just before the activity starts interacting with the user.
     
  4. 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.
     
  5. onStop()

    This method is called once the activity is no longer visible or activity is supposed to destroy.
     
  6. onDestroy()

    This is called before the activity is destroyed by the system.
     
  7. onRestart()

    This is called when the activity restarts after stopping it.
 

The above diagram shows the complete activity lifecycle of an android application. It works by the 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.
  1. Running - In this state, activity is visible and interacts with the user.
  2. Paused – Activity is partially visible but might be killed by the system.
  3. Stopped – Activity is not visible, the instance is running but might be killed by the system.
  4. Killed – Finally, activity has been terminated by calling finish() method.
Example of Android activity lifecycle.
  1. package com.example.ppandey.myapplication;    
  2.     
  3. import android.os.Bundle;    
  4. import android.support.design.widget.FloatingActionButton;    
  5. import android.support.design.widget.Snackbar;    
  6. import android.support.v7.app.AppCompatActivity;    
  7. import android.support.v7.widget.Toolbar;    
  8. import android.util.Log;    
  9. import android.view.View;    
  10. import android.view.Menu;    
  11. import android.view.MenuItem;    
  12.     
  13. public class MainActivity extends AppCompatActivity {    
  14.     
  15. @Override    
  16. protected void onCreate(Bundle savedInstanceState) {    
  17.    super.onCreate(savedInstanceState);    
  18.    setContentView(R.layout.activity_main);    
  19.    Log.d("Android Lifecycle""onCreate Invoked");    
  20. }    
  21. public void onStart()    
  22. {    
  23.    super.onStart();    
  24.    Log.d("Android Lifecycle","onStart Invoked");    
  25. }    
  26. public void onResume()    
  27. {    
  28.    super.onResume();    
  29.    Log.d("Android lifecycle","onResume Invoked" );    
  30. }    
  31. public void onPause()    
  32. {    
  33.    super.onPause();    
  34.    Log.d("Android Lifecycle","onPause Invoked");    
  35. }    
  36. public void onStop()    
  37. {    
  38.    super.onStop();    
  39.    Log.d("Android lifecycle","onStop Invoked");    
  40. }    
  41. public void onRestart()    
  42. {    
  43.    super.onRestart();    
  44.    Log.d("Android lifecycle","onRestart Invoked");    
  45. }    
  46. public void onDestroy()    
  47. {    
  48.    super.onDestroy();    
  49.    Log.d("Android lifecycle","onDestroy Invoked");    
  50. }    
  51.     
  52. }   
    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