What is an Activity?

In Android an activity represents a single screen or window with a user interface. It is like window or frame of Java.The android Activity is the subclass of ContextThemeWrapper class. By the help of activity, you can place all your UI components or widgets in a single screen. Android activity is the subclass of ContextThemeWrapper class. Activity User Interface defines in XML file and the background programming of that UI construct in Activity Class (Java File).

Android Activity Lifecycle

Before Launching, an Activity go through some events and each event has corresponding methods which are called at that event. Android Activity Life Cycle is controlled by 7 methods of Android App Activity class.As the user navigate within apps or leave/re-enter to your app activity call a sequence of callback methods to maintain the state of activity. Here we describe 7 lifecycle events and callbacks of Activity that how they behave at different states.

 

onCreate()

You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state. In the onCreate() method, you perform basic application start-up logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate() might bind data to lists, initialize background threads, and instantiate some class-scope variables.

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

This method receives the parameter savedInstanceState, which is a Bundle object containing the activity's previously saved state. If the activity has never existed before, the value of the Bundle object is null.

onStart()

When the activity enters the Started state, android calls the onStart() method which makes the activity visible to the user and prepares the activity to enter the foreground and become interactive.

onResume()

When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user.

The app stays in this state until user take focus away from the app. For Ex. user navigating to another activity, or the device screen's turning off.

As user left the screen the activity enters in the Paused() state, and the system invokes the onPause() callback.

onPause()

The system calls this method as the first indication that the user is leaving your activity.

You can use this method to release some system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.

onStop()

When your activity is no longer visible to the user, it has entered the Stopped state.

onRestart()

When you comes back to your activity then it first call the onRestart callback and then onStart()again().

onDestroy()

Called before the activity is destroyed due to calling finish() method or application termination.

 

Graphical Flow Layout of Activity Life Cycle

below image show how an activity switches between multiple states.



Let's create a simple example to catch the Activity Events.

Lets view a example which demonstrate the multiple events before resuming the activity on screen.

Your xml Layout should look like this..

	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="dzone.jaipur.myapp1.MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="63dp"
        android:layout_marginTop="52dp"
        android:text="Hello World" />



Write this code in your java File (Android Activity).


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		 Log.d("lifecycle","onCreate invoked");  
    }  

    @Override  
    protected void onStart() {  
        super.onStart();  
         Log.d("lifecycle","onStart invoked");  
    }  

    @Override  
    protected void onResume() {  
        super.onResume();  
         Log.d("lifecycle","onResume invoked");  
    }  

    @Override  
    protected void onPause() {  
        super.onPause();  
         Log.d("lifecycle","onPause invoked");  
    }  

    @Override  
    protected void onStop() {  
        super.onStop();  
         Log.d("lifecycle","onStop invoked");  
    }  

    @Override  
    protected void onRestart() {  
        super.onRestart();  
         Log.d("lifecycle","onRestart invoked");  
    }     

    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
         Log.d("lifecycle","onDestroy invoked");

		
	}

}


Now Run the Application and get the desired output in Log File.