Activity's lifecycle in Android Application.

In this tutorial you can find information about lifecycle of an Android Activity. Almost all the Activities interact with the user. So, an activity creates a window where you can place any of the user interface elements. Understanding of the Activity's lifecycle is a necessary thing for any Android developer. The whole lifecycle of the Activity can be presented on the following schema: Lifecycle This schema shows in a good way what happens with the activity during it's lifecycle. The most important thing about activity's lifecycle is the fact that Android developer can manage the processes that happens during activity's life. We will create a demo application to see how does the activity works. For this scope, please create new project with blank activity. We will not modify the layout files and simply will add GUI components in the activity class:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView  
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     android:text="@string/hello"
  11.     />
  12. </LinearLayout>
The GUI Elements used in this demo application are:
  1. LinearLayout linLayout;
  2. Button closeBtn;
The first thing that happens with the activity is it's creation. The initialization of all GUI elements is performed in the
  1. public void onCreate(Bundle savedInstanceState)
method. You can add any modification to this method only after you call the same method from the super class:
  1. super.onCreate(savedInstanceState);
After this we can add our code:
  1. super.onCreate(savedInstanceState);
  2.                 linLayout = new LinearLayout(this);
  3.                 setContentView(linLayout);
  4.                 linLayout.setOrientation(LinearLayout.VERTICAL);
  5.                 closeBtn = new Button(this);
  6.                 closeBtn.setText("CLOSE");
  7.                 linLayout.addView(closeBtn);
  8.                 closeBtn.setOnClickListener(new OnClickListener() {
  9.                         @Override
  10.                         public void onClick(View v) {
  11.                                 ((Activity) v.getContext()).finish();
  12.                         }
  13.                 });
  14.                 Toast.makeText(this, "Demo Activity created!", Toast.LENGTH_SHORT).show();
The action listener is added to the close button.It will call the finish method for this activity. After activity is created, onStart() method is called:
  1. @Override
  2.         public void onStart() {
  3.                 super.onStart();
  4.                 Toast.makeText(this, "Demo Activity Started!", Toast.LENGTH_SHORT).show();
  5.         }
After onStart() method onResume() method is called:
  1. @Override
  2.         public void onResume() {
  3.                 super.onResume();
  4.                 Toast.makeText(this, "Demo Activity resumed!", Toast.LENGTH_SHORT).show();
  5.         }
Note an important thing that you always need to call method of the super class to avoid any kinds of errors. Now the activity is running and you can interact with the user. The interaction with the user in this program is presented by close button. This button closes activity. Let's take a look what happens when the activity is closed. Firstly, onPause() method is closed.We will add the message as in the previous cases:
  1. @Override
  2. public void onPause() {
  3.         super.onPause();
  4.         Toast.makeText(this, "Demo Activity paused!", Toast.LENGTH_SHORT).show();
  5.         }
After this activity is stopped:
  1. @Override
  2.         public void onStop() {
  3.                 super.onStop();
  4.                 Toast.makeText(this, "Demo Activity stoped!", Toast.LENGTH_SHORT).show();
  5.         }
There is a possibility to restart Activity even if is stopped.This step is controlled by onRestart() method:
  1. @Override
  2.         public void onRestart() {
  3.                 super.onRestart();
  4.                 Toast.makeText(this, "Demo Activity restarted!", Toast.LENGTH_SHORT).show();
  5.         }
The onDestroy() method kills the activity. You have to add any code before calling the super class onDestroy() method:
  1. @Override
  2.         public void onDestroy() {
  3.                
  4.                 Toast.makeText(this, "Demo Activity destroyed!", Toast.LENGTH_SHORT).show();
  5.                 super.onDestroy();
  6.         }
Now you know the basic things about managing Android Activity's lifecycle. This mechanism of overriding can be used for different purposes. For example, you can save data of application when it's destroyed to a database and restore in onCreate(Bundle savedInstanceState) method.

Add new comment