Android Activity Lifecycle

Activity Lifecycle or Architecture Component

What is Activity Lifecycle?

The life cycle of activity is a series of states that an action may be in during its entire life cycle. From the moment it is created until it is destroyed, its resources are restored by the system. When users interact with their apps and other apps on the device, the operation will switch to a different state.

What are the different stages of the activity life cycle?
As a result, there are four states of an Activity (App) on Android: Active, Paused, Stopped, and Destroyed. The action is either visible, partially visible, or invisible from the user’s perspective at any given time.

Methods of Activity Lifecycle :

MethodsDescription
onCreate()Called when the activity is created. @ OnCreate is only used for the initial creation and therefore only needs to be called once.
onStart() When the activity becomes visible to the user, the onStart () method is called. This calls just after the onCreate () method
onResume()The activity is paused and is expected to resume shortly. There are several reasons an activity can enter this state.
onPause()onPause. Called when the activity is still partially visible, but the user is likely to leave their activity entirely
onStop()onStop() If your activity is no longer visible to the user, it will become Stopped and the system calls the onStop() callback.
onRestart()OnRestart() is called when the user presses the home button and reaches the home screen. In this case, the activity will not be destroyed and a pause/stop event will be triggered. When the user reopens your app, onRestart() will be called for the activity before onCreate().
onDestroy()onDestroy () is a method that is called when your activity is shut down. Called to allow your activity to complete all shutdowns

Method of Activity Lifecycle

–>the code given below is given in kotlin language.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("TAG","Activity is  Created")
    }

    override fun onStart() {
        super.onStart()
        Log.i("TAG","Activity  is  Start")
    }
    
    override fun onPause() {
        super.onPause() 
        Log.i("TAG","Activity is  Pause")
    }
    
    override fun onResume() {
        super.onResume() 
        Log.i("TAG","Activity is  Resume")
    }
    
    override fun onStop() {
        super.onStop()
        Log.i("TAG","Activity is  Stop")
    }
    
    
    override fun onRestart() {
        super.onRestart() 
        Log.i("TAG","Activity is  Restart")
    }
    
    override fun onDestroy() {
        super.onDestroy()
        Log.i("TAG","Activity is Destroy")
    }
  • We hope that this guide will assist you in quickly understanding an Android activity lifecycle or Android Component. If you have any problems or queries, please post them in the comments section and we will gladly assist you.

Introduction to Fragment in Android with Example | Android development

This Post Has One Comment

Leave a Reply