[Android]: Understanding Activity Lifecycle for Beginners-Part 1

Richa Sharma
Globant
Published in
6 min readFeb 8, 2021
pic credit-Google
  • Android Activity is very important component and first thing to be known as android developer.

What is an Activity ?

  • Everything what user see on mobile app screen is an Activity.An activity provides the window in which the app draws its UI.
  • Our daily use Mobile application have multiple screens for example, suppose we have an application where user first login or register and on save button click user navigates to home screen of application. These all are different screens which perform different operations, which means our mobile app contains multiple activities which user see on screen.
  • To provide this user experience, you should know how to manage component lifecycle. Android Lifecycle have different callback methods, for example if user open an application create method in lifecycle gets call, if you are using music app and you want to switch to open new application to see notification message than new callback for pause gets call, so each callback allows you to perform specific work that’s appropriate to a given change of state.

Activity-lifecycle concepts :

  • An Android activity undergoes through a number of states during its whole lifecycle.
  • Block diagram to understand stages of lifecycle :-
pic credit — developer.android
  • Let’s go step by step to understand each method -
  • 1. onCreate( )- This method gets called when activity is first created. In this method you perform basic application startup logic that should happen only once for the entire life of the activity.It is fundamental setup for the activity, such as declaring the user interface.
  • Example :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("lifecycle","onCreate invoked");
}
  • In this example, the XML layout file is specified by passing file’s resource ID R.layout.activity_main to setContentView().What setContentView() does the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to ViewGroup.LayoutParams#MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.
  • Now , as you can see I have used Log.d() in code to understand when onCreate() gets call. So I have created simple program which has Button “click me” which is first screen user can see i.e MainActivity
Application shows for onCraete(), onStart(), onResume()
  • I open my application and on this my first Activity which is Main Activity gets open and with this OnCreate() gets call.
  • 2. onStart() — This method gets called when activity gets visible to the user.When the activity enters the Started state, the system invokes this callback.this method is where the app initializes the code that maintains the UI.
override fun onStart() {
super.onStart()
Log.d("lifecycle","onStart invoked");
}
  • 3. onResume() — This method gets called when activity start interacting with user.
override fun onResume() {
super.onResume()
Log.d("lifecycle","onResume invoked");
}
  • After an activity started, restarted (onRestart() happens before onStart()), or paused (onPause()), onResume() called. When the activity is in the state of onResume(), the activity is ready to be used by the app user.
  • If you want to update data, make a data update function and put the function inside the onResume(). Or put a loadData function inside onResume().
  • 4. onPause() — this method gets call when activity is no longer visible to user.You can just override onPause() in your activity say MainActivity when you are navigating to another activity SecondActivity and override onResume() when u come back to the MainActivity.
override fun onPause() {
super.onPause()
Log.d("lifecycle","onPause invoked activity 2");
}
  • 5. onStop() — this method gets called when activity is no longer visible to user.
override fun onStop() {
super.onStop()
Log.d("lifecycle","onStop invoked");
}
  • 6. onDestroy() — this method gets called before activity destroyed.The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.
override fun onDestroy() {
super.onDestroy()
Log.d("lifecycle","onDestroy invoked");
}
  • 7. onRestart() —This method gets called before activity destroyed. onRestart() is called after onStop() when the current activity is being re-displayed to the user.
override fun onRestart() {
super.onRestart()
Log.d("lifecycle","onRestart invoked");
}
  • Let’s understand with sample program steps-
Understanding of onPause, onStop

Now as you can see above with above gif I have created a simple program in which screen 1 with button “Click me” MainActivity gets called first and on button click the user gets navigates to new Activity which is with Text “Go Back” SecondActivity.

  • On this process of navigation we have invoked different methods :
  • (a). on click of application we can see Screen Click me MainActivity
  • 1. onCreate() — MainActivity (first activity)gets called
  • 2. onStart() — MainActivity
  • 3. onResume() — MainActivity()

(b.)Now on press of ClickMe user navigate to new Screen i.e SecondActivity on this

  • 1. onPause() — MainActivity()
  • 2. onCreate() — SecondActivity gets called
  • 3. onStart() — SecondActivity
  • 4. onResume() — SecondActivity
  • 5. onStop() — MainActivity

If user click on Text “Go Back” of SecondActivity than

  • 1. onPause- SecondActivity
  • 2. onCreate() — MainActivity gets called
  • 3. onStart() — MainActivity
  • 4. onResume() — MainActivity
  • 5. onStop() —SecondActivity

Main Activity Code:



class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("lifecycle", "onCreate invoked Activity 1");
val button: Button = findViewById(R.id.btnFindMe)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}

override fun onStart() {
super.onStart()
Log.d("lifecycle", "onStart invoked Activity 1");
}

override fun onResume() {
super.onResume()
Log.d("lifecycle", "onResume invoked Activity 1");
}

override fun onPause() {
super.onPause()
Log.d("lifecycle", "onPause invoked Activity 1");
}

override fun onStop() {
super.onStop()
Log.d("lifecycle", "onStop invoked Activity 1");
}

override fun onDestroy() {
super.onDestroy()
Log.d("lifecycle", "onDestroy invoked Activity 1");
}

override fun onRestart() {
super.onRestart()
Log.d("lifecycle", "onRestart invoked Activity 1");
}

}

Code for Second Activity —




class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second_activity)
Log.d("lifecycle", "onCreate invoked activity 2");
val txtClick: TextView = findViewById(R.id.txtClickMe)
txtClick.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}

override fun onStart() {
super.onStart()
Log.d("lifecycle", "onStart invoked activity 2");
}

override fun onResume() {
super.onResume()
Log.d("lifecycle", "onResume invoked activity 2");
}

override fun onPause() {
super.onPause()
Log.d("lifecycle", "onPause invoked activity 2");
}

override fun onStop() {
super.onStop()
Log.d("lifecycle", "onStop invoked activity 2");
}

override fun onDestroy() {
super.onDestroy()
Log.d("lifecycle", "onDestroy invoked activity 2");
}

override fun onRestart() {
super.onRestart()
Log.d("lifecycle", "onRestart invoked activity 2");
}
}
  • main activity xml-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click me"></Button>

</RelativeLayout>
  • second activity xml-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtClickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • I have used this simple code in which we can understand the activity lifecycle flow.
  • If you are beginner and learning Android I would recommend you to go step by step to understand the activity lifecycle flow.Understand each step and practice it with flow and keep it simple

You have to work hard to keep it simple and still have meaning.

credit — Google
  • Happy Coding :)

--

--