The Problem with Activities2:03 with Ben Deitch
The goal of this course is to learn about the Android activity lifecycle. In this video we'll see why the activity lifecycle is such an important topic. Let's get started!
[MUSIC] 0:00 Hi, I'm Ben. 0:04 In this course, we're going to learn about the Activity Lifecycle. 0:06 In the Android operating system, almost all apps start and end with an activity. 0:11 Activities get created, paused, stopped and eventually, 0:17 destroyed and it's our responsibility as developers 0:21 to make sure that we handle each of these lifecycle changes gracefully. 0:25 There's a bunch of different lifecycle methods but before we get into that, 0:30 let's see why it's so important that we manage the activity lifecycle. 0:34 Here's the fun facts project from the very first course. 0:39 Let's see what happens when we get a new fun fact and then rotate the app. 0:43 Instead of showing the new fact, it's showing the first fact. 0:48 It turns out, activities are entirely recreated when the orientation changes. 0:52 At first glance, this probably seems a little weird but since different 0:58 orientations require different layouts, and those layouts require 1:02 different resources, activities are always recreated on an orientation change. 1:07 Since Android handles recreating the activity, 1:13 all we have to handle is saving and restoring our activity state. 1:16 For example, if a user is halfway down a giant list and 1:21 they rotate the app, normally, the activity will be restarted, and 1:25 they'll be looking at the top of the list. 1:30 But if we hook into the right lifecycle events, 1:33 we can save off their position in the list when the activity stops and 1:35 restore that position when it gets recreated. 1:39 That way, the user will be totally unaware of what just happened behind the scenes. 1:42 In the rest of this course, we'll see how we can use a bundle to 1:48 handle these kinds of changes, how we can save data using shared preferences and 1:51 we'll finish with a project that will you a chance to show off what you've learned. 1:56 It's a lot to cover. 2:00 Let's get started. 2:02
You need to sign up for Treehouse in order to download course files.Sign up