Activity lifecycle in android with example?

What is an Activity in android?

An activity represents a single screen with a user interface it's just like a window or frame.

Whenever you open an Android application, then you see some UI over your screen. That screen is called an Activity.

Activity is represents the presentation layer of an Android application, e.g. a screen which the user see. An Android application can have several activities and it can be switched between them during runtime of the application.

There are  seven life-cycle method of Activity describes how activity will behave at different states.



Android Activity Life cycle methods and Callback:-

  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onRestart()
  7. onDestroy()
You must be aware of that a program starts from a main() function in most of programming languages. Similarly, Android initiates the program from activity with a calling method is onCreate() callback method. There is a sequence of callback methods that starts  from Top to bottom with different  methods shown in the above Activity life cycle diagram.

  • onCreate():- When the system first creates the activity, In this state, the activity is created.
  • onStart():- When this callback method is called when the activity becomes visible to the user.
  • onResume():- When the activity enters the Resumed state, it comes to the foreground,when activity will start interacting with the user.
  • onPause():- When activity is no longer in the foreground, activity is not visible to the user.
  • onStop():-When your activity is no longer visible to the user.
  • onRestart():- When activity is Stopped state, the activity either comes back to interact with the user.
  • onDestory():- When called before the activity is destroyed.

Declare activities in Manifest file:-

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mukesh.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />

</RelativeLayout>

MainActivity.kt

package com.mukesh.myapplication

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

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("activity_lifecycle","onCreate invoked")
}

override fun onStart() {
super.onStart()
Log.d("activity_lifecycle","onStart invoked")
}

override fun onResume() {
super.onResume()
Log.d("activity_lifecycle","onResume invoked")
}

override fun onPause() {
super.onPause()
Log.d("activity_lifecycle","onPause invoked")
}

override fun onStop() {
super.onStop()
Log.d("activity_lifecycle","onStop invoked")
}

override fun onRestart() {
super.onRestart()
Log.d("activity_lifecycle","onRestart invoked")
}

override fun onDestroy() {
super.onDestroy()
Log.d("activity_lifecycle","onDestroy invoked")
}


}

Disqus Comments