Activity lifecycles are crucial to understand so as to control what happens with your app as the user moves into, through, and away from it, gets interrupted by phone calls or rotates their phone. In this article, we are going to look at the activity lifecycle and see when different calls are made in response to different events. This awareness will help you when beginning with app development so you understand where you may need to place logic in light of different actions the user is taking.
This flow chart from the Android Developers documentation shows activity flow through the activity lifecycle . As we can see, there are seven functions responsible for the activity: onCreate(), onStart(), onResume(), onRestart(), onPause(), onStop(), and onDestroy(). Let’s talk about common use cases that send us through different paths within this flow chart.
Opening and Closing an App
Before the app is opened, the activity does not exist. So, when opening the app, the onCreate(), onStart() and onResume() functions all run in succession to make the activity visible and usable by the user. Think of these functions as creating the activity, making it visible to the user, and then making it have focus (so that the user can interact with it) . The activity is now running. Now if the app is closed, the onPause(), onStop(), and onDestroy() functions are all called. The activity has been shut down.
Rotating a Phone
Surprisingly, the act of rotating a phone actually kills and restarts the activity. So, the order of calls is onPause(), onStop(), onDestroy(), onCreate(), onStart() and onResume(). Why is this important to realize? Let’s imagine you are filling out a form and then you rotate your screen to finish. If the data that you have already typed is not saved somewhere and then reaccessed when the activity reloads, the act of rotating your screen will clear all of that data. Have you noticed in the onCreate() method we always override in every activity we make there is a parameter called savedInstanceState? Well this is where you can store that data during these cycle changes. More information about instance state will be for another blog post, but it is important to know that these lifecycle changes are happening so you know when and where you will need to store and retrieve this data.
Moving from One Activity to Another
Let’s say the user presses a button that creates an intent and moves us to another activity. In this case, the first activity is not fully destroyed. onPause() is run on the first activity. Since the second activity did not exist yet, then we must call the trio of functions to create and present that to the user: onCreate(), onStart() and onResume(). Finally, now that the second activity is up and running, the first activity’s onStop() function is run.
Moving Back to the First Activity
Now, the user presses the back button in the second activity to go back to the first. In this case, that second activity is being fully dismissed, and so first onPause() executes. Once the first activity starts back up using onRestart(), onStart() and onResume(), then the second activity can finish its process with onStop() and onDestroy() .
Opening and Closing a Dialog Activity
If a user clicks a button within the first activity that opens a dialog activity, we see a different set of events. Because the first activity is still visible, yet just not able to be interacted with, we ONLY run onPause() within the first activity. The dialog activity starts with our normal onCreate(), onStart() and onResume() functions. The first activity cannot run onStop() because although it is obstructed and not in focus, it is still partially visible . When the dialog is dismissed, the dialog activity runs the onPause(), onStop(), and onDestroy() functions, while the first activity simply runs onResume().
Receiving a Call
A user should be able to receive a call while your app is running. In this case, your activity will be placed in the background while the call app handles the call. Similarly to moving from one activity to another, your app will call onPause() and onStop(). When the user has finished the call and gone back to your app, your activity will run onRestart(), onStart() and onResume(). This is the same thing that will happen if the user navigates to another app without closing yours, or if the user presses the home button.
Now that we understand the activity lifecycle, we can better utilize the lifecycle to meet expectations in regards to user performance. Our next blog will be discussing fragments and the fragment lifecycle and how they differ from activities and the activity lifecycle.
 “The Activity Lifecycle”, Oct 4, 2021. Accessed on: Oct 20, 2021. [Online]. Available: https://developer.android.com/guide/components/activities/activity-lifecycle
 D. Buketa, Android Lifecycle, Razeware, May 3, 2021. Accessed on: Oct 20, 2021. [Online] Available: https://www.raywenderlich.com/21382977-android-lifecycle#toc-anchor-004
 “Android activity lifecycle: state order when new activity starts”, May 19, 2021. Accessed on: Oct 20, 2021. [Online]. Available: https://newbedev.com/android-activity-lifecycle-state-order-when-new-activity-starts
 T. Campbell, Activity Lifecycle, Dartmouth, Jan 29, 2016. Accessed on: Oct 20, 2021. [Online] Available: https://www.cs.dartmouth.edu/~campbell/cs65/lecture05/lecture05.html