Activity Lifecycle Overview6:23 with Ben Deitch
It's time for a deep dive into the Android activity lifecycle. In this video we'll learn about the various states our activity can exist in, and we'll go over each activity lifecycle method.
Now that we've seen why it's so 0:00 important to handle are activities lifecycle, let's dive a little deeper. 0:02 In Android, activities are managed using an activity stack. 0:06 When a new activity is started, it's placed on top of the stack and 0:11 becomes the running activity. 0:15 In addition to running, 0:17 there are two other states that our activity can exist in. 0:19 If our activity is visible but 0:22 is not on top of the activity stack, it is considered paused. 0:24 This usually happens because the activity on top of the stack either doesn't take up 0:28 the whole screen, or has some transparency which lets our activity show through. 0:33 If no part of our activity is visible, it's considered stopped. 0:38 But why do we care about what state our activity is in? 0:43 The key thing that we need to be aware of is that Android will regularly 0:46 kill stopped activities to free up memory for other applications. 0:51 Imagine that you were playing a game. 0:55 It takes up a lot of system memory and you're really far along in a certain task. 0:57 Then a phone call from your mom comes in and you have to answer it. 1:02 The call shows up on the top of the stack, forcing your game to a stopped state. 1:06 But uh-oh, Android needs more memory to handle the phone call, so 1:11 it destroys the stopped activity for your game. 1:16 If the game developers didn't properly manage the state of the game and 1:19 the activity lifecycle, then all your progress would be lost. 1:23 Again, the main take away is that stopped activities will eventually be 1:28 killed by the operating system. 1:32 In earlier versions of Android, 1:34 Android would even kill paused activities if the need for memory was dire enough. 1:36 Add to that, that Android will destroy and restart our activities on 1:41 every device rotation and we've got a lot of uncertainty that we need to handle. 1:45 Luckily, Android provides us with seven lifecycle methods which we can 1:50 override to make handling activity lifecycle changes a lot easier. 1:54 Here we are in the activity class reference on the Android developer site. 1:59 I put the link in the teacher's notes. 2:03 There's a section here called the activity lifecycle and 2:06 it shows when each lifecycle method will be called. 2:10 Let's take a deeper look at this to get a better understanding of things. 2:15 When we launch our activity, the first method to be called is onCreate. 2:20 OnCreate is used to perform any initial startup procedures, like 2:25 initializing variables, creating views or starting any background processes. 2:29 OnCreate also provides a bundle containing the activity's previous instance state 2:34 if there was one or a null bundle, otherwise. 2:39 When onCreate is finished, Android will call the onStart method. 2:43 OnStart represents the point in the activity's lifecycle right 2:47 before it is visible to the user and not yet 2:51 on top of the activity stack, meaning the user can't yet interact with it. 2:53 It's rare that you would need to override the onStart method but 2:59 you should still be aware of it. 3:02 After onStart, Android will immediately call onResume, 3:05 onResume represents the point where the activity is 3:08 finally on top of the activity stack and ready to interact with the user. 3:12 A typical task for onResume would be starting any animations in our activity. 3:17 After onResume returns, the activity is running. 3:22 It's on top of the activity stack and the user is able to interact with it. 3:25 From here, the activity can go into one of two directions. 3:30 If we leave the activity by using the back button, we are telling Android that we 3:34 are done with this activity and onPause, onStop and onDestroy will all be called. 3:38 If we leave the activity in any other way like the Home or 3:45 Recent Apps button, then our activity will go to the background. 3:48 Either way, the lifecycle event after onResume will be onPause. 3:52 OnPause is called when another activity is taking over the top of the activity stack 3:58 and is becoming the foreground activity. 4:03 This is typically where we want to commit any unsaved changes and 4:05 stop any animations. 4:09 If the activity in front of ours allows our activity to show through, 4:11 then when that activity is dismissed, our activities onResume method will be called. 4:15 Otherwise, if the activity in front of ours completely covers our activity so 4:21 that it's no longer visible, 4:25 then our activity will continue with calling the onStop method. 4:27 OnStop is called when our activity enters the stop state and is no longer visible. 4:32 Like it's counterpart onStart, it's rare that you would need to override onStop. 4:37 Later on when we learn about services and broadcast receivers, 4:43 we might find some good reasons to override these two lifecycle methods. 4:46 But for now, it's just good to know they exist, and 4:50 what points in the activity lifecycle they represent. 4:53 After onStop, if the user returns to the activity and 4:55 were lucky enough to not have been killed in the meantime, 5:00 onRestart will be called, followed by onStart. 5:02 All we need to know about onRestart is that it comes after onStop. 5:07 There are very few cases where you would need to override onRestart. 5:11 The last activity lifecycle method is onDestroy. 5:15 This is usually called because the user has exited the activity by the back 5:19 button, or in a way where we explicitly called the activities finish method. 5:22 Or because the activity was stopped and 5:27 is now being destroyed by the operating system to reclaim memory. 5:29 A typical use case for onDestroy would be to clean up any background processes 5:33 started in the onCreate method. 5:37 That was a lot to go over. 5:41 Before we move on, let's do a quick recap of when each lifecycle method is called. 5:43 OnCreate is the entry point to our activity and is called first. 5:49 OnStart is called right before the activity becomes visible. 5:53 OnResume is called when the activity is in the foreground and 5:57 the user can interact with it. 6:01 OnPause is called when another activity comes into the foreground. 6:03 OnStop is called when our activity is no longer visible. 6:08 OnRestart is called when our activity is being started again and 6:12 onDestroy is called when the user is done with our activity or 6:16 when Android destroys our activity to reclaim memory. 6:20
You need to sign up for Treehouse in order to download course files.Sign up