 Hey everybody, this is Brian. Welcome to the second Android tutorial. Today we're going to be going over activities and the application lifecycle. If you've kind of gone through some of Android's developers getting started guide, you've heard a lot about activities and lifecycle, but you don't really know what they are and it takes a little while for these to really sink in. They have some really good information here, but you know if you're just walking into this it can be a little scary. I would actually recommend you try to do some of their stuff. It has some really great information, but we're going to kind of flesh out the application lifecycle. So let's go ahead and, where's my graphic? There it is. If you've watched other tutorials, you've probably seen this graphic. It's pretty standard. This is just the lifecycle of an application. Now an activity starts. Notice how it doesn't say the application starts. It says the activity starts. That's very important. What is an activity? Well an activity is the screen that the user sees. You can have multiple activities per application. So this lifecycle isn't just an application lifecycle. It's an activity lifecycle. Your application can actually run without any activities at all. Not really true, but you know what I mean. You can do a full service that just runs in the background. All right. So the first thing in this is it goes on create, on start, on resume. Now why does it create start and resume? There is a process here that you need to understand. And that is the activity is running, but it may not always be running. For example, the user may lock their phone and put it in their pocket. The user may switch to another application. Android may go into low power mode and say, hey, we need to kill all non-needed applications. So to accommodate those things, you need to understand where you are in the lifecycle here. And one of those is start and resume. Create is when it's created. Start obviously is when it's started. Resume is when it's resumed. If these seem pretty intuitive, it's because they're meant to be by design. The ones you really need to focus on are resume and pause. You notice how you're resumed, activities running, and then pause. They switch to a different activity, a different application. They lock their phone, but then they come back and the activity comes back to the foreground. They switch back. They turn their phone back on, whatever. You go to resume. So this is meant to be your data right here. This is where your data will be persisted. So on pause is when you would save. On resume is when you would load. And let's kind of illustrate that with an actual program real quick here. So we're going to start a new program. And we're going to call this life. And then company domain, I just have voidromes.com, but you can just put whatever you have. Now, we want this to be phone tablet, although you can do pretty much anything else. Minimum SDK. This is very important. Notice how at 4.0.3, this will run on approximately 97.4% of the devices out there. Now, if you click this help me choose, you get this beautiful graphic that really illustrates the user base along Android. And you can see oldest to newest, you have a larger installation base. So very few people are running 6.0, only 4% of the phones out there. That tells you not just that users don't really update their phones, but the manufacturers don't push out the newest and greatest and greatest. So be aware of that. So you're going to want to choose something kind of middle of the line here, so that you get, I shouldn't say middle line, something older but acceptable like ice cream sandwich or jelly beans. So you have a large installation base that can use this. We're just going to go with API 15 Android 4.0.3 that made date this video. We're going to hit next. Now, notice how we get what type of activity would you like. And we're just going to say empty activity. And we're going to leave this as main activity and it'll say layout activity underscore main. And we'll explain all this, but we're just going to hit finish. Hit grinds away in the background. All right. Your program structure is very simple. You have your manifest. Now, notice how the first element actually the main element here is application. And then you have some child elements in this XML, the activity. And you have some attributes here that really define, you know, allow backup, the icon, et cetera, et cetera. Each activity will have its own element in here and will be named appropriately. And this one it's dot main activity. Now intent filters. This is a little bit of explanation that needs to happen. Not only do you have activities, which is the screen the user sees and just to solidify that, I'll show you what our activity looks like going to layouts. This is the activity we've just created. And it has this beautiful little hello world. And let's just, you know, play around with this for a minute. Let's make that really big. So as this beautiful hello world, this is the activity. This is the screen the user is going to see. And that itself is also just XML. Notice how we can drag and drop or we can do the text, but point being it has an intent filter. So you need to understand what an intent is. Well, an intent is a class. It's a non visual class. And it is well self explanatory. It's an intent you intend to do something. Intents are really used as messages between Android and the application and between activities. So for example, let's say you have an activity. This guy and just let's pretend we had a button. We click that it will create a new activity. Well, there will be an intent between those two. And we'll cover that in a future tutorial. But for now, just understand that somewhere there is this mythical class called an intent. And those can be filtered based on what you're trying to do. So the action for this intent filter is main, meaning this is our main activity. And the category is launcher. So because it's a main launcher, this will be launched when we launch the phone. Android will look for the first instance of a main launcher activity and start that as your introduction activity, if you will. All right. So we're going to do a little bit of work here. Android, util, oops, log. And let's just private static string. Let's call this tag equals foiteromes. And what we're going to do here is we're going to use the log class. And you know how there's a d in any, a d is for debug, e is for error. And we're going to use the tag that we just defined up there. And then our actual message. Now the tag is important because it lets you filter these messages so you know what you're looking at. So you could have a tag that says dog cat, my program, my TCP socket, whatever you want. All right. So we are going to just save that. And we're going to give this a good build. You see down here, Gradle is build running, and it's just going to build it. If there's any error messages, we'll get a big little pop up saying there are errors, but of course there's no errors. And we are going to actually go into our Android device manager, we're going to create a new device and we're going to just choose Nexus 5. Why? Why not? And I'm actually going to do the newest, latest, and greatest Nougat just because you could choose any of these other, but I'm going to do Nougat just because I like the name Nougat. And let's just call this testing. You can choose the orientation, etc., etc. We're just going to hit finish. And this will create our virtual device. And we're going to start this. And you can see down here, it's actually starting the virtual device. And once the emulator boots up, it'll take a minute. This is what I mean by emulator is really slow. But this is an actual Android emulator. I mean, it's running full Android. I shouldn't say full. People get mad when I say full. There are some very big limitations to this such as, you know, not being able to connect to Wi-Fi, but it's running Android and it will run your program. So we're going to move that off to the side. We are going to go to our monitor here and make sure that our emulator is actually selected because I have my physical phone plugged in. So our emulator is selected. And we can see monitoring in real time with log cap. Now you see how we're not doing anything. And it's still kicking out logs because we're not filtering. So we want to edit the filter configuration. And let's just call this void realms. I thought I heard something anyways. So we're going to filter off that tag right here. Now that tag is the same thing that we're using in the log D. See, that's how you could actually filter things out. So when we filter, notice how poof, they all go away. So we're going to run this thing. And we're going to choose our emulator that's currently running. And let's just bring this back over here and you'll see it when it runs. It'll index, it'll install the APK and it'll launch the main activity. And you can see how it's running a little slow because it is an emulator. Hello world. And we also have a log called create. Now if we go back to our life cycle here, you can see that's the first thing that's called is create. So we're going to flesh out the rest of these. They start, resume, pause, stop, destroy and restart just so we can see what happens. And instead of typing all these out, you can actually just do control o and then just start selecting. So what do we say? You know, start, stop, destroy, resume, pause. There's restart in there somewhere. I probably just went way past it. I'm lazy. We'll just do this through the magic of copy and paste. All right. So first thing you should do notice is that it's calling super on start. Super means obviously Java. We're extending something. This is inheritance. This is something that I kind of fell into when I first started Android programming. A lot of the older tutorials will not say app compact activity. It'll just say activity or app activity. I don't remember. I think it just said activity and none of the examples were working. It was driving me crazy. And that's because they switched and they made a newer version of it. So we're using the app compact activity for these tutorials. And we're going to just copy and paste these bad boys out here. We'll see destroy. Wow, that's embarrassing. Resume, pause, come on mouse, you weren't pause and restart save our work. And you notice how it's still running. We got this little button here. So if we go here, notice how it's still running. So we're going to actually stop that. Make sure our monitors up. Let's clear that. And we're going to run this. Make sure it's on the testing. And you notice how it went much faster. It's because the bulk of the APK was just there. And I think it just uploaded to Delta. Don't quote me on that. That's pure speculation on my part. But we have the create, start, resume. And if we look at, sure enough, create, start, resume. So it's following the activity life cycle here. Our activity is running. So let's just take this out of the foreground. And we should see pause, stop and destroy, maybe. Well, we only saw pause and stop because we haven't actually destroyed it. It's still sitting in the background. We know that because we can go here and there it is. And if we select it, we see we have done restart, start, resume. Now, what are all these things mean? Why is that so confusing? Just simply doing that, we did restart, start, resume. Because that's part of the life cycle here. This is where you need to choose when to save your data and persist it. For example, on pause, the application's pausing. On stop means the application's just stopped. It's much like a VCR pause, stop. So when you hit stop, you need to make sure that data is saved. Typically, I'll do pause and resume just to be safe. But you need to make sure that data is saved somewhere. Now on restart, that's when we're starting this thing. And you want to make sure that your data is loaded and then start any animations and then you're actually resuming. So that's part of the activity life cycle right here. And if we just, you know, close this thing out, maybe. There we go. You'll see destroy. That means the application is, I should say the activity is no longer running. And each activity, remember, because you can have, oops, more than one of these activities, you can have thousands if you want. Each one of these is going to go through this life cycle. Now, let's clear this. One thing you should be aware of, and this is one of those little gotchas with Android. We're going to restart this bad boy. You can see how we have create, start, resume. Well, let's clear this. And all we're going to do is we're going to simply change the orientation of this application. We're just going to rotate the phone if you will. Now you notice how when we rotated that phone, it went through part of that life cycle. It paused, stopped, destroyed, keyword destroyed, created, started and resumed. So it actually destroyed that activity. It just wiped it right out of memory and then recreated it from scratch. Why does it do that? Well, Android is designed to extend battery life. It's designed to make this device run faster, smoother, better. And it actually takes a lot less energy to recreate it than it does to manipulate everything on the screen and move it around and calculate it all out. So what this thing is actually doing is it's going back into the code here and it's saying set content r.layout.mainActivity and say, hey, load that into memory with the current orientation. So what is this r layout? r stands for resource. Anything that's in the res folder basically gets a special little variable in here. So we have r.layout. So if we follow this, just call resource r, r layout activity main. That's how it knows what to load. It's loading this file right here. Pretty simple, pretty easy little caveat that you got to understand what's going on. So some key takeaways from this video is understand that your activity will go through the lifecycle at any point. Just because you think it's running doesn't mean it's going to stay running. The user may change the orientation. The user may put it into the background. The user can do all sorts of crazy, funky things. Another thing you really need to be aware of is just basically how to log. This is going to be a real lifesaver for you going through these tutorials. Understand what the manifest is and what an activity is. And remember, the activity is just a screen that the user sees. And know that there's an intent class. An intent is a message class. You intend to do something. And those can be filtered. And that's how it's really determining how this is launched. You have the main and launcher. Other than that, we really just talked about the r variable with the resource layouts. So that's really it for this tutorial. I hope you found this educational and entertaining. Before you take off, be sure to visit void realms.com. I'm going to have the source code for this and the other tutorials out in here. I will actually make an Android section. And if you click on contact, you'll see this link. Be sure to join the void realms Facebook group. There's over 1000 programmers out there from all walks of life, multiple languages. And if you have a question, anybody in there, myself included can help you out.