Activity Life Cycle

About understanding of Activity Life Cycle in Android Studio

Activity life cycle
Photo by Tolga Ulkan on Unsplash

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create a Blank Activity and click on Finish button.

Step 2 : Open res -> layout ->activity_main.xml and add following code :

< RelativeLayout xmlns : android = “http://schemas.android.com/apk/res/android"

xmlns : tools = “http://schemas.android.com/tools"

android : layout_width = “match_parent”

android : layout_height = “match_parent” >

< TextView

android : layout_width = “wrap_content”

android : layout_height = “wrap_content”

android : text = “@string/hello_world” / >

< / RelativeLayout >

Step 3 : Open src -> package ->MainActivity.java and add following code :

package com . skholingua . android . activitylifecycle ;

import android . app . Activity ;

import android . os . Bundle ;

import android . widget . Toast ;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate ( Bundle savedInstanceState ){

super . onCreate ( savedInstanceState ) ;

setContentView ( R . layout . activity_main ) ;

Toast . makeText ( getBaseContext ( ) , “onCreate triggered” , 0 ) . show ( ) ;

}

/** Called when the activity is about to become visible. */

@Override

protected void onStart ( ) {

super . onStart ( ) ;

Toast . makeText ( getBaseContext ( ) , “onStart triggered” , 0 ) . show ( ) ;

}

/** Called when the activity has become visible. */

@Override

protected void onResume ( ) {

super . onResume ( ) ;

Toast . makeText ( getBaseContext ( ) ,”onResume triggered” , 0 ) . show ( ) ;

}

/** Called when another activity is taking focus. */

@Override

protected void onPause ( ) {

super . onPause ( ) ;

Toast . makeText ( getBaseContext ( ) , “onPause triggered” , 0 ) . show ( ) ;

}

/** Called when the activity is no longer visible. */

@Override

protected void onStop ( ) {

super . onStop ( ) ;

// I usually don’t use toast here

Toast . makeText ( getBaseContext ( ) , “onStop triggered” , 0 ) . show ( ) ;

}

/** Called just before the activity is destroyed. */

@Override

public void onDestroy ( ) {

super . onDestroy ( ) ;

// I usually don’t use toast here

Toast . makeText ( getBaseContext ( ) ,”onDestory triggered” , 0 ) . show ( ) ;

}

}

Step 4 : Open AndroidManifest.xml and add following code :

<? xml version = “1.0” encoding = “utf-8” ?>

< manifest xmlns : android = “http://schemas.android.com/apk/res/android"

package =”com.skholingua.android.activitylifecycle”

android : versionCode = “1”

android : versionName = “1.0” >

< uses — sdk

android : minSdkVersion = “16”

android : targetSdkVersion = “19” / >

< application

android : allowBackup = “true”

android : icon = “@drawable/ic_launcher”

android : label = “@string/app_name”

android : theme = “@style/AppTheme” >

< activity

android : name = “.MainActivity”

android : label = “@string/app_name” >

< intent — filter >

< action android : name =”android.intent.action.MAIN” / >

< category android : name =”android.intent.category.LAUNCHER” / >

< / intent — filter >

< / activity >

< / application >

< / manifest >

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store