Android

 

  1. package example.javatpoint.com.activitylifecycle;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.         Log.d("lifecycle","onCreate invoked");  
  14.     }  
  15.     @Override  
  16.     protected void onStart() {  
  17.         super.onStart();  
  18.         Log.d("lifecycle","onStart invoked");  
  19.     }  
  20.     @Override  
  21.     protected void onResume() {  
  22.         super.onResume();  
  23.         Log.d("lifecycle","onResume invoked");  
  24.     }  
  25.     @Override  
  26.     protected void onPause() {  
  27.         super.onPause();  
  28.         Log.d("lifecycle","onPause invoked");  
  29.     }  
  30.     @Override  
  31.     protected void onStop() {  
  32.         super.onStop();  
  33.         Log.d("lifecycle","onStop invoked");  
  34.     }  
  35.     @Override  
  36.     protected void onRestart() {  
  37.         super.onRestart();  
  38.         Log.d("lifecycle","onRestart invoked");  
  39.     }  
  40.     @Override  
  41.     protected void onDestroy() {  
  42.         super.onDestroy();  
  43.         Log.d("lifecycle","onDestroy invoked");  
  44.     }  
  45. }  

Output:

You will not see any output on the emulator or device. You need to open logcat.


MethodDescription
onCreatecalled when activity is first created.
onStartcalled when activity is becoming visible to the user.
onResumecalled when activity will start interacting with the user.
onPausecalled when activity is not visible to the user.
onStopcalled when activity is no longer visible to the user.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed.