‫-: Hi.
‫Within this lecture, we are going to look into
‫a very important concept in Android development,
‫that is activity lifecycle.
‫So we have talked about activities a little bit right?
‫So let me create a new project,
‫because, we want a project with activity this time,
‫and I'm gonna start with an empty activity,
‫remember we had started with no activity before,
‫but this time, I actually need an activity.
‫So, I'm going to call this "Methods and Classes" okay?
‫Later on after activity lifecycle, we're gonna
‫deep dive into methods and classes.
‫It really doesn't matter right now whether you
‫choose for a minimum API level, just choose one.
‫Make sure you choose the AndroidX artifacts,
‫and choose JAVA, and then you're more than welcome
‫to click finish, and create your project.
‫So, that's what we gonna do.
‫We gonna look into the activity lifecycle,
‫and then we will move on to methods, and then classes,
‫a little bit of object-oriented programming,
‫and then we're done.
‫So, over here,
‫when we deal with an activity, we deal with a screen,
‫the user can interact with right?
‫But remember, when we first create an activity,
‫when we get this main activity, that JAVA file,
‫what do we see?
‫If you say, onCreate?
‫Then you're correct.
‫So onCreate is a method, and we don't know really
‫what a method is, but you can understand it right?
‫We don't know how to create our own methods right now,
‫but, you can understand what the method does.
‫So we talked about onCreate.
‫It's called when the activity is created, right?
‫So, there are actually a lot of other methods
‫like this as well, and we're going to deep dive into them.
‫So, make sure you open your main activity,
‫and make sure you see mainactivity.java
‫on the right hand side.
‫Remember we have this main XML,
‫where we do the design and stuff, and we can actually
‫open the title bar from here as well.
‫In fact within this lecture, we're not gonna do much,
‫on the XML side, but rather we're gonna focus
‫on the coding side.
‫Okay, so we were talking about actually this.
‫So this is onCreate.
‫And this is called, even before the user sees the activity,
‫even before the user sees, the screen itself.
‫So whatever you write over here will be done,
‫and the user will see it when they first open the app,
‫or when they first open the activity.
‫So this our only activity
‫inside of our application right now.
‫So when user taps on the app icon, they will be presented
‫with main activity, and whatever you write under onCreate,
‫will be already executed before they even see it.
‫But sometimes, maybe we may want to do some other things.
‫Depending on the current state of our activity.
‫For example, we can do something like when a user see it.
‫Or when a user closes it down.
‫So, we should use something else rather than onCreate,
‫over here, and that is where we leverage
‫the activity lifecycle methods.
‫In order to that, you should know how activities work,
‫how activities have their own lifecycle.
‫So, in order to show you, I'm going to go over to Google,
‫okay, and I'm going to go into the actually,
‫developer.Android.com, we have already seen it.
‫If you search for Android activity lifecycle,
‫like this as you can see there are a lot of results,
‫like millions of results, but the first one
‫will be the developer.Android.com,
‫so as you can see it's titled like
‫"Understand the Activity Lifecycle", and it actually
‫instructs, or it actually displays the information
‫that we need to know in order
‫to understand the cycle.
‫You don't have to go into Google, or you don't have to
‫open that, I'm gonna show you anyway.
‫I'm trying to show you developer.Android.com, is always
‫a good idea.
‫So over here we see a basic chart,
‫where we can keep track of what's going on,
‫when we interact with an activity.
‫So as you can see, it takes us from the beginning
‫of an activity, to the end of an activity.
‫Of course, we can have multiple activities in our app.
‫Right now, we only have one,
‫but it can have it's own lifecycle, as well.
‫Just imagine that.
‫User taps on the app, it opens the screen,
‫and they close it down, close the app down.
‫Just go into the other app, or close the phone down,
‫I don't know.
‫So, activity actually kind of pauses, or kind of stops,
‫depending on the situation.
‫So there are actually other methods to handle
‫that kind of situation as well.
‫For example, onStop.
‫As you can see there are a lot of methods actually.
‫And it starts with onCreate.
‫So if you follow along this route,
‫you follow along at the actual path of what
‫an activity goes through in life.
‫So, it starts with onCreate, it continues with onStart,
‫onResume, and this where our activity is running.
‫So, if you come over here
‫you can see that after onResume activities running,
‫and onPause, when the user pauses this activity,
‫the activity will not be visible after awhile,
‫and it goes into onStop.
‫So, if the user doesn't see an activity,
‫it's maybe in onStop state.
‫And if you destroy an activity, the activity actually
‫shuts down, it's destroyed, it's no more.
‫So, when you create the activity one more time,
‫then it's created, so onCreate will get called.
‫But if you don't destroy it,
‫the activity would be in onStop state,
‫but then, it goes into the onStart methods.
‫And if it's onPause, it goes into the onResume method.
‫You can actually track the arrows
‫in order to understand this,
‫or you can do what I'm about to do, over here.
‫So what I'm about to do,
‫is to call the other methods over here.
‫So make sure you go out of this onCreate method,
‫not inside of curly braces, and write onResume for example,
‫and you can just double click on that to call the
‫original method, just like this.
‫So, let's call the other one, onStop like that,
‫okay, and let's call onPause as well,
‫so as you can see, we can call whatever we want,
‫within the activity lifecycle methods.
‫They're all built in, the only thing you have to do
‫is just type them inside of the class,
‫outside of any method,
‫and they will be presented to you.
‫When you double click on them,
‫you can actually override them.
‫Overriding means changing the content of a built in,
‫or earlier defined method, okay?
‫So if I want something to happen,
‫in onCreate like printing out something,
‫then, I would have written it over here like,
‫onCreate called, or executed, okay?
‫So I'm going to copy this, and I'm going to paste it
‫to the onResume as well, and I'm gonna say "onResume called"
‫and I'm going to do the same thing for onStop okay?
‫And, I'm gonna do the same thing for onPause.
‫So, that's cool, that's how we identify,
‫what is being called, and when it's being called.
‫So right now, if I track the logcat,
‫I would understand
‫when, these methods are being executed.
‫So, let me open the emulator over here,
‫so see my app, and in fact I won't see much,
‫because I didn't change anything in the XML,
‫but it would enable me to track
‫what's going on, in my logcat.
‫So, you know how this works.
‫All we have to do is just wait until this opens,
‫okay, this emulator fires up like this,
‫and then go into the logcat,
‫from here,
‫and, in order to only see the things
‫that we are printing out,
‫you're more than welcome to filter this out,
‫okay, as you can see there are kind of levels over here
‫to show us like, only errors or,
‫only information,
‫or only warnings.
‫If you select variables, you see everything okay?
‫And it's a little bit crowded as you can see.
‫In order to make sure you're follow along,
‫with your own onResume, onStop thing,
‫you can actually filter those things,
‫by saying like onCreate, okay?
‫And as you can see now onCreate called,
‫it's being printed because onCreate has already
‫been called, even before we see the activity.
‫So, in order to assure that everything
‫is in visibility, I'm going to filter this system.alt,
‫and as you can onResume, is already called as well.
‫Remember, the chart, before we see the activity,
‫onResume is already called.
‫Now, we're gonna try and see the onPause or onStop,
‫so in order to do that,
‫I'm going to close down
‫this activity, by clicking this.
‫As you can see onPause is called, and onStop is called.
‫Now, activity is not actually visible to the user.
‫Kind of visible, but it's actually not,
‫but if I click on it again, onResume is called.
‫And, beware that onCreate, hasn't been called.
‫Again, only onResume is called.
‫Because we were on onStop,
‫and activity wasn't even destroyed,
‫so onCreate hasn't been called.
‫So, this is tricky because if I want something to happen
‫every time a screen shows up,
‫then I should write it under onResume,
‫not onCreate right?
‫Because onCreate, isn't being called
‫once activity is created.
‫So, that's a very important point.
‫We're gonna stop here and continue within the next one.