1 00:00:00,000 --> 00:00:04,479 [MUSIC] 2 00:00:04,479 --> 00:00:06,197 Hi, I'm Ben. 3 00:00:06,197 --> 00:00:11,080 In this course, we're going to learn about the Activity Lifecycle. 4 00:00:11,080 --> 00:00:17,140 In the Android operating system, almost all apps start and end with an activity. 5 00:00:17,140 --> 00:00:21,610 Activities get created, paused, stopped and eventually, 6 00:00:21,610 --> 00:00:25,650 destroyed and it's our responsibility as developers 7 00:00:25,650 --> 00:00:29,100 to make sure that we handle each of these lifecycle changes gracefully. 8 00:00:30,320 --> 00:00:34,820 There's a bunch of different lifecycle methods but before we get into that, 9 00:00:34,820 --> 00:00:38,210 let's see why it's so important that we manage the activity lifecycle. 10 00:00:39,390 --> 00:00:42,010 Here's the fun facts project from the very first course. 11 00:00:43,040 --> 00:00:46,880 Let's see what happens when we get a new fun fact and then rotate the app. 12 00:00:48,080 --> 00:00:51,210 Instead of showing the new fact, it's showing the first fact. 13 00:00:52,220 --> 00:00:56,470 It turns out, activities are entirely recreated when the orientation changes. 14 00:00:58,030 --> 00:01:02,650 At first glance, this probably seems a little weird but since different 15 00:01:02,650 --> 00:01:07,450 orientations require different layouts, and those layouts require 16 00:01:07,450 --> 00:01:12,330 different resources, activities are always recreated on an orientation change. 17 00:01:13,590 --> 00:01:16,500 Since Android handles recreating the activity, 18 00:01:16,500 --> 00:01:20,170 all we have to handle is saving and restoring our activity state. 19 00:01:21,850 --> 00:01:25,860 For example, if a user is halfway down a giant list and 20 00:01:25,860 --> 00:01:30,370 they rotate the app, normally, the activity will be restarted, and 21 00:01:30,370 --> 00:01:32,080 they'll be looking at the top of the list. 22 00:01:33,090 --> 00:01:35,770 But if we hook into the right lifecycle events, 23 00:01:35,770 --> 00:01:39,830 we can save off their position in the list when the activity stops and 24 00:01:39,830 --> 00:01:42,640 restore that position when it gets recreated. 25 00:01:42,640 --> 00:01:46,730 That way, the user will be totally unaware of what just happened behind the scenes. 26 00:01:48,280 --> 00:01:51,570 In the rest of this course, we'll see how we can use a bundle to 27 00:01:51,570 --> 00:01:56,480 handle these kinds of changes, how we can save data using shared preferences and 28 00:01:56,480 --> 00:02:00,890 we'll finish with a project that will you a chance to show off what you've learned. 29 00:02:00,890 --> 00:02:02,350 It's a lot to cover. 30 00:02:02,350 --> 00:02:03,010 Let's get started.