Activity Lifecycle in Android

Activity Life Cycle is part of Android Studio. Activity lifecycle works in a different method. There are seven methods of activity lifecycle. Let's discuss all methods of the Android activity lifecycle.

  1. OnCreate() 
  2. OnStart()
  3. OnResume()
  4. OnRestart()
  5. OnPause()
  6. OnStop()
  7. OnDestroy()


1.OnCreate(): It is called when the activity is first created. This method is used to create a view, binding view in activity.

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

 

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

 

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

 

}}

2.OnStart(): It is called when the activity is visible to the user. This method is called after the onCreate() method.

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

}

override fun onStart() {

super.onStart()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}}

3.OnResume(): This method calls after the onStart() method . Also called when activity background and move other activity.

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

}

 

override fun onStart() {

super.onStart()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onResume() {

super.onResume()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

}

4.OnRestart(): This method is also called when the application is in the background and the application resumes.

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

}

 

override fun onStart() {

super.onStart()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onResume() {

super.onResume()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onRestart() {

super.onRestart()

Toast.makeText(this@MainActivity, "onRestart Called", Toast.LENGTH_LONG).show()

}

}

5.OnPause(): This method is also called when the application is Pause() or moved to the background app. 

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

}

 

override fun onStart() {

super.onStart()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onResume() {

super.onResume()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onRestart() {

super.onRestart()

Toast.makeText(this@MainActivity, "onRestart Called", Toast.LENGTH_LONG).show()

}

 

override fun onPause() {

super.onPause()

Toast.makeText(this@MainActivity, "onPause Called", Toast.LENGTH_LONG).show()

}

}

6.OnStop(): This method is also called when the application is OnStop()  moved to the background app or the application is killed.

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

}

 

override fun onStart() {

super.onStart()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onResume() {

super.onResume()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()}

override fun onRestart() {

super.onRestart()

Toast.makeText(this@MainActivity, "onRestart Called", Toast.LENGTH_LONG).show()

}

 

override fun onPause() {

super.onPause()

Toast.makeText(this@MainActivity, "onPause Called", Toast.LENGTH_LONG).show()

}

 

override fun onStop() {

super.onStop()

Toast.makeText(this@MainActivity, "onStop Called", Toast.LENGTH_LONG).show()

}

}

7. OnDestroy(): This method is also called when the mobile application is OnDestroy() moved to the application killed.

import android.os.Bundle

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

 

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

Toast.makeText(this@MainActivity, "onCreate Called", Toast.LENGTH_LONG).show()

}

 

override fun onStart() {

super.onStart()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onResume() {

super.onResume()

Toast.makeText(this@MainActivity, "onStart Called", Toast.LENGTH_LONG).show()

}

 

override fun onRestart() {

super.onRestart()

Toast.makeText(this@MainActivity, "onRestart Called", Toast.LENGTH_LONG).show()

}

 

override fun onPause() {

super.onPause()

Toast.makeText(this@MainActivity, "onPause Called", Toast.LENGTH_LONG).show()

}

 

override fun onStop() {

super.onStop()

Toast.makeText(this@MainActivity, "onStop Called", Toast.LENGTH_LONG).show()

}

 

override fun onDestroy() {

super.onDestroy()

Toast.makeText(this@MainActivity, "onDestroy Called", Toast.LENGTH_LONG).show()

}

}

Happy Coding!


365Bloggy May 10, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive