Basically, the one screen of mobile is knows as Activity in Android.
Android Activity Lifecycle is controlled by 7 methods which are included in android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper 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 GUI components or widgets in a single screen.
The activity goes from one state to another which is known as activity lifecycle.
Methods Of Activity Lifecycle
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. |
Life Cycle Diagram
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tejsumeru.lifecycle"> <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"?> <android.support.constraint.ConstraintLayout 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:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
MainActivity.xml
Toast displays the pop up menu with short text message. The makeText() method is used with Toast, and at last show() method is used to show pop up.
package com.tejsumeru.lifecycle import android.support.v7.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.e("Activity","OnCreate") } override fun onStart() { super.onStart() Log.e("Activity","OnStart") } override fun onResume() { super.onResume() Log.e("Activity","OnResume") } override fun onPause() { super.onPause() Log.e("Activity","OnPause") } override fun onStop() { super.onStop() Log.e("Activity","OnStop") } override fun onDestroy() { super.onDestroy() Log.e("Activity","OnDestroy") } override fun onRestart() { super.onRestart() Log.e("Activity","OnRestart") } }
OutPut
Now its Done… Keep It Up