1 00:00:00,000 --> 00:00:04,461 [MUSIC] 2 00:00:04,461 --> 00:00:09,028 All right, now that we've got a pretty good idea of what the activity life cycle 3 00:00:09,028 --> 00:00:12,300 is and when each of the life cycle methods will be called, 4 00:00:12,300 --> 00:00:16,350 let's jump into a sample app and run a few tests to see these in action. 5 00:00:17,490 --> 00:00:21,350 Here's the app we'll be using to test out the activity life cycle. 6 00:00:21,350 --> 00:00:25,610 Before we get started let's take a minute to see how the app works. 7 00:00:25,610 --> 00:00:29,490 First, there's a custom activity called LoggingActivity. 8 00:00:29,490 --> 00:00:32,520 Every activity that extends LoggingActivity 9 00:00:32,520 --> 00:00:35,450 will log a message each time a lifecycle method is called. 10 00:00:36,540 --> 00:00:38,550 Next we have MainActivity. 11 00:00:39,785 --> 00:00:42,770 MainActivity is the entry point of the application. 12 00:00:42,770 --> 00:00:45,660 And has two buttons that launch the other two activities. 13 00:00:46,660 --> 00:00:49,230 One button launches opaque activity and 14 00:00:49,230 --> 00:00:51,400 the other button launches transparent activity. 15 00:00:52,680 --> 00:00:55,221 MainActivity, OpaqueActivity, and 16 00:00:55,221 --> 00:00:59,750 TransparentActivity all extend the LoggingActivity. 17 00:00:59,750 --> 00:01:02,180 So we should see a log message for each and 18 00:01:02,180 --> 00:01:05,220 every life cycle method that gets called for these activities. 19 00:01:06,400 --> 00:01:08,185 Let's run the app and see what gets called. 20 00:01:13,008 --> 00:01:17,749 Before we start parsing the log cat, let's filter it by our tag, life cycle example. 21 00:01:23,850 --> 00:01:29,947 Just as we would expect from the diagram, the methods called so 22 00:01:29,947 --> 00:01:35,950 far are onCreate, onStart and onResume from MainActivity. 23 00:01:35,950 --> 00:01:41,510 And MainActivity is currently in a running state at the top of the activity stack. 24 00:01:41,510 --> 00:01:44,310 What do we expect to happen if we launch OpaqueActivity? 25 00:01:46,300 --> 00:01:51,280 If OpaqueActivity is launched, MainActivity won't be visible anymore. 26 00:01:51,280 --> 00:01:55,680 So we would expect to see onPause and onStop from MainActivity. 27 00:01:56,900 --> 00:02:00,508 And for OpaqueActivity to be the running activity, 28 00:02:00,508 --> 00:02:07,060 we would expect to see onCreate, onStart, and onResume from OpaqueActivity. 29 00:02:07,060 --> 00:02:09,480 But what will be the order of these methods? 30 00:02:10,720 --> 00:02:11,880 Take a minute to think about it. 31 00:02:13,000 --> 00:02:14,580 Feel free to pause me, I'll be right here. 32 00:02:17,170 --> 00:02:19,910 All right, got your guess? 33 00:02:19,910 --> 00:02:20,710 Let's find out. 34 00:02:25,291 --> 00:02:30,104 If you guessed MainActivity is onPause, followed by onCreate, 35 00:02:30,104 --> 00:02:33,867 onStart, and onResume from OpaqueActivity, and 36 00:02:33,867 --> 00:02:38,690 finishing up with MainActivity is onStop, then you're right. 37 00:02:39,920 --> 00:02:42,765 Let's take a minute to see why this is the case. 38 00:02:42,765 --> 00:02:46,950 OnPause is called when you can no longer interact with an activity. 39 00:02:46,950 --> 00:02:51,530 This being called first, means main activity is removed from the top of 40 00:02:51,530 --> 00:02:56,620 the activity stack, which is necessary if we're going to have a new activity on top. 41 00:02:56,620 --> 00:02:59,890 Next comes onCreate, onStart. 42 00:02:59,890 --> 00:03:03,235 And onResume, from OpaqueActivity. 43 00:03:03,235 --> 00:03:07,510 OpaqueActivity is now on top of the activity stack, and ready for 44 00:03:07,510 --> 00:03:09,400 user interaction. 45 00:03:09,400 --> 00:03:13,075 Lastly, MainActivities on stop is called. 46 00:03:13,075 --> 00:03:16,910 OnStop is called when the activity is no longer visible. 47 00:03:16,910 --> 00:03:21,470 Since OpaqueActivity isn't visible until after onStart and 48 00:03:21,470 --> 00:03:25,640 since onResume will immediately follow onStart. 49 00:03:25,640 --> 00:03:29,560 MainActivity's onStop method won't be called until after 50 00:03:29,560 --> 00:03:32,440 OpaqueActivity's onResume method. 51 00:03:32,440 --> 00:03:35,933 Now that we know which life cycle methods were called to get us here, 52 00:03:35,933 --> 00:03:37,466 let's hit the back button and 53 00:03:37,466 --> 00:03:41,233 see which life cycle methods get called when we go back to Main Activity. 54 00:03:43,775 --> 00:03:46,332 Looks like it's the same calls as last time, 55 00:03:46,332 --> 00:03:49,780 except we've switched MainActivity and OpaqueActivity. 56 00:03:51,600 --> 00:03:56,110 The only difference is the call to onDestroy and OpaqueActivity. 57 00:03:57,440 --> 00:04:01,010 Leaving an activity by hitting the back button is our way of telling 58 00:04:01,010 --> 00:04:03,090 Android that we're finished with an activity. 59 00:04:04,330 --> 00:04:09,200 That's why Android destroys OpaqueActivity instead of leaving it in a stopped state. 60 00:04:10,250 --> 00:04:12,780 Now that we're back in MainActivity, 61 00:04:12,780 --> 00:04:15,525 let's see what happens when we launch the TransparentActivity. 62 00:04:16,830 --> 00:04:19,670 Go ahead and pause me if you'd like to take a guess at what happens. 63 00:04:21,440 --> 00:04:21,940 Here we go. 64 00:04:25,366 --> 00:04:29,995 Turns out, it's exactly like when we launched the OpaqueActivity 65 00:04:29,995 --> 00:04:33,280 except OnStop isn't called in MainActivity. 66 00:04:33,280 --> 00:04:37,590 Since we can still see main activity behind our TransparentActivity. 67 00:04:37,590 --> 00:04:41,940 MainActivity is on a pause state and onStop isn't called. 68 00:04:43,130 --> 00:04:44,500 Cool. 69 00:04:44,500 --> 00:04:47,930 For the last test, let's see what happens when we hit the home button. 70 00:04:49,920 --> 00:04:53,250 First, TransparentActivity is pause. 71 00:04:53,250 --> 00:04:58,320 And then, both MainActivity and TransparentActivity are stopped. 72 00:04:59,510 --> 00:05:00,920 When we hit the Home button, 73 00:05:00,920 --> 00:05:04,030 we're telling Android that we're moving on to something else. 74 00:05:05,070 --> 00:05:08,100 Android doesn't know if we'll be back or not, so 75 00:05:08,100 --> 00:05:12,850 it keeps our activities around in a stopped state, in case we return. 76 00:05:12,850 --> 00:05:17,680 Keep in mind that Android may kill stopped activities to free up memory. 77 00:05:17,680 --> 00:05:20,330 You've learned a lot about the activity lifecycle. 78 00:05:20,330 --> 00:05:24,770 In the next video we'll learn how we can hook into our activities lifecycle 79 00:05:24,770 --> 00:05:26,860 to save data using shared preferences. 80 00:05:27,870 --> 00:05:32,240 Also, if you'd like to try these tests on your own, or are curious about how this 81 00:05:32,240 --> 00:05:36,750 app is programmed there's a link to the GitHub project in the teacher's notes. 82 00:05:36,750 --> 00:05:39,340 And it's included below in the project files as well.