Skip to main content

Understanding the Life Cycle of a Activity

1. Using Android Studio , create a new Android project and name is Activity101.

2. In the Activity101Activity.java file , add the following highlighted statements.


Package com.example.activity101;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity
{
       String tag="LifeCycle Step";
      @Override
       protected void onCreate(Bundle savedInstanceState)
       {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
               Log.d(tag,msg:"In the onCreate () event");
       }
       public void onStart()
       {
                super.onStart();
                Log.d(tag,msg:"In the onStart () event");
       }
       public void onRestart()
       {
                super.onRestart();
                Log.d(tag,msg:"In the onRestart () event");
       }
       public void onPause()
       {
                super.onPause();
                Log.d(tag,msg:"In the onPause () event");
       }
       public void onStop()
       {
                super.onStop();
                Log.d(tag,msg:"In the onStop () event");
       }
       public void onDestroy()
       {
                super.onDestroy();
                Log.d(tag,msg:"In the onDestroy () event");
       }
}

3. Press Shift + F9 to debug the application , or select Run -> Debug . Then select one of your Android Virtual Devices from the pop-up window.

4. When the activity is first loaded , you should see something very similar to the following in the logcat console . If you do not see the logcat console , click Android Monitor at the bottom of the Android Studio window:

11-16 06:25:59.369: D/Lifecycle step(559) : In the onCreate() event
11-16 06:25:59.369: D/Lifecycle step(559) : In the onStart() event
11-16 06:25:59.369: D/Lifecycle step(559) : In the onResume() event

5. If you click the Back button on the Android emulator , you see the following :

11-16 06:29:26.665: D/Lifecycle step(559) : In the onPause() event
11-16 06:29:28.465: D/Lifecycle step(559) : In the onStop() event
11-16 06:29:28.465: D/Lifecycle step(559) : In the onDestroy() event

6. Click the Home button , click the Overview icon , select the Android101 application , and observe the following :

11-16 06:25:59.369: D/Lifecycle step(559) : In the onCreate() event
11-16 06:25:59.369: D/Lifecycle step(559) : In the onStart() event
11-16 06:25:59.369: D/Lifecycle step(559) : In the onResume() event

7. Click the Home button and then click the Phone button on the Android emulator so that the activity is pushed to the background . Observe the output in the logcat window:

11-16 06:29:26.665: D/Lifecycle step(559) : In the onPause() event
11-16 06:29:28.465: D/Lifecycle step(559) : In the onStop() event

8. Notice that the onDestroy() event is not called , indicating that the activity is still in memory . Exit the phone dialer by clicking the Back button . The activity is now visible again. Observe the output in the logcat window :

11-16 06:29:28.465: D/Lifecycle step(559) : In the onRestart() event
11-16 06:29:28.465: D/Lifecycle step(559) : In the onStart() event
11-16 06:29:28.465: D/Lifecycle step(559) : In the onResume() event

The onRestart () event is now fired , following by the onStart() and onResume () methods.

How It Works 

As you can see from this simple example , an activity is destroyed when you click the Back button. This is crucial to understand because whatever state the activity is currently in will be lost . This means you need to write additional code in your activity to preserve its state when the activity is destroyed . At this point , note that the onPause ( ) method is called in both scenarios :
  • When an activity is send to the background
  • When a user kills an activity by tapping Back button
When an activity is started , the onStart() and onResume () methods are always called , regardless of whether the activity is restored from the background or newly created . When an activity is created for the first time , the onCreate () method is called .

From the preceding example , you can derive the following guidelines:
  • Use the onCreate() method to create and instantiate the objects that you will be using in your application.
  • Use the onResume() method to start any services or code that needs to run while your activity is in the foreground.
  • Use the onPause() method to stop any services or code that does not need to run when your activity is not in foreground.
  • Use the onDestroy() method to free up resources before your activity is destroyed.
NOTE Even of an application has only one activity and the activity is killed , the application is still running in memory.

Comments

Popular posts from this blog

What is Android

Android is a moblie operating System that is based on a modified version of Linux. It was originally developed by a startup of the same name , Android , Inc. In 2005 , as part of its strategy to enter the moblie space , Google purchased Android , Inc. and took over its development work (as well as its development team). Google wanted the Android OS to be open and free , so most of the Android code was released under the open source Apache License. That means anyone who wants to use Android can do so by downloading the full Android source code. Moreover, vendors ( typically hardware manufacturers ) can add their own proprietary extensions to Android and customize Android to differentiate their products from others . This development model makes Android very attractive to vendors, especially those companies affected by the phenomenon of Apple's iPhone,which was a hugely successful product that revolutionized the smartphone industry. When the iPhone was launched , many smartphone m...

Android Studio

The first and most important piece of software you need to download is Android Studio. After you have downloaded and installed Android Studio , you can use the SDK Manager to download and install multiple versions of the Android SDK. Having multiple version of the SDK available enables you to write programs that target different devices. For example , you can write one version of an application that specifically targets Android Nougat , but because that flavor Android is on less than 1% of devices, with multiple versions of the SDK you can also write a version of your app that uses older features and targets Marshmallow or Lollipop users. You can use Android Device Manager to set up device emulators. You can Download Android Studio from https://developer.android.com/studio Android Studio is packaged in an executable. Run the install process to set up Android Studio. After you've download and run the setup executable, use the following steps to go through the installation proce...

Debugging Your Application

After you have built an application , you need to be able to debug it and see what is going on inside your code . One of the handiest ways to be able to see inside your code it through the use of break points. Breakpoints allow you to pause the execution of your code at specific location and see what is going on (or what is going wrong). Let's take a look at how to use breakpoints in Android Studio. Setting Breakpoints Breakpoints are mechanism by which you can tell Android Studio to temporarily pause execution of your code , which allows you to examine the condition of your application . This means that you can check on the values of variables in your application while you are debugging it. Also , you can check whether certain lines of code are being executed as expected - or at all. To tell Android Studio that you want to examine a specific line of code during debugging , you must set breakpoint at the line. Click the margin of the editor tab next to line of code you want...