Android tutorial - Android Activity Lifecycle | android lifecycle - android app development - android studio - android development tutorial



Android activity lifecycle

Learn android - android tutorial - Android activity lifecycle - android examples - android programs

What is Android Activity

  • An activity represents a single screen with a user interface just like window or frame of Java .Android activity is the subclass of Context Theme Wrapper class.
    • Activities are a fundamental building block of Android applications and they can exist in a number of different states.
    •  life cycle of activity in android
    • The activity lifecycle begins with instantiation and ends with destruction, and includes many states in between.
    • When an activity changes state, the appropriate lifecycle event method is called, notifying the activity of the impending state change and allowing it to execute code in order to adapt to that change.
    • Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class.
    • An activity is the single screen in android. It is like window or frame of Java.
    • By the help of activity, you can place all your UI components or widgets in a single screen.
    • The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods

  • Let's see the 7 lifecycle methods of android activity.
Method Description
onCreate called when activity is first created.
onStart called when activity is becoming visible to the user.
onResume called when activity will start interacting with the user.
onPause called when activity is not visible to the user.
onStop called when activity is no longer visible to the user.
onRestart called after your activity is stopped, prior to start.
onDestroy called before the activity is destroyed.
 oncreate in android
  • It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.
    • File: MainActivity.java
package com.example.activitylifecycle;  
import android.os.Bundle;  
import android.app.Activity;  
import android.util.Log;  
import android.view.Menu;  
public class MainActivity extends Activity {  
    @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");  
    }  
}  
click below button to copy the code from android tutorial team
  • You will not see any output on the emulator or device. You need to open logcat.
 android adb driver
  • Now see on the logcat: on Create, on Start and on Resume methods are invoked.
 android add widget
  • Now click on the HOME Button. You will see on Pause method is invoked.
 android intent example
  • After a while, you will see on Stop method is invoked.
 activity android
  • Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
 activity in android
  • Now click on the lifecycle activity icon.
 android broadcastreceiver
  • Now see on the logcat: on Restart, on Start and on Resume methods are invoked.
 androidmanifest xml
  • If you see the emulator, application is started again.
 alogcat
  • Now click on the back button. Now you will see onPause methods is invoked.
 android activity life cycle
  • After a while, you will see onStop and onDestroy methods are invoked.
 life cycle of android

Related Searches to android lifecycle | Android Activity Lifecycle