Okay, so in the last video we saw how to
use the Android Studio logcat, and how
useful it is to see a log entry when
your functions are called. So now we're
going to add logging to all the
lifecycle functions. Doing that will let
us see exactly what happens when we do
various 'Androidy' things while our app's
running. We'll put the
screen to sleep, for example, and also
switch to another application while ours
is running. Now there's quite a few
functions that we need to add, but
fortunately we don't have to do a lot of
typing, and that's because Android Studio
can generate the basic functions for us.
So I'm going to start by creating a new
line here, after the onCreate method
because that's where our code's going to
go. And what we're going to do then, is
come up here and select the Code menu,
or click the Code menu, and there's this
optional over here for Generate, which is
a code generation. And note the shortcut
on a Mac is command n and alt insert on
a PC so you can access it that way in
the future. So I'm going to click on that
now, and that opens up a little popup
menu down here, and you can see that
there's various options on code that
we can get Android Studio to generate
for us. The one we want at this time is
Override Methods, and that's probably the
the one that you'll be using
most often. You can see that Override and
Implement have actually got their own
keyboard shortcuts, if you want to get to
those in the future. Now I'm going
to select override methods, and that
actually brings up a huge list. Alright,
so the first one that I want would be on
Destroy, so I could come all the way down
and try and find that. So we can scroll
down to find that method.
There it is there, so I can actually click on
that and then I could hold the ctrl key
down and scroll through the list,
selecting all the ones I wanted. But that
can get tedious, searching manually, and it
also inserts the methods in whatever
order it feels like. Now, although the
order that they appear in the code
doesn't matter at all, as far as the
computer's concerned, I'd like them to
appear in the order that they're called,
just so that we get used to that order.
So luckily, we can actually start typing
a method and Android Studio will find
it in the list. So I can start typing on
Start, and I can then use the down
arrow to jump to the next matching
method. So I can click on the down arrow
and you can see it's now on the right
method. So once I've done that, I can then
press ENTER to insert the onStart
method, so I'm going to do that. You can see the
onStart method is showing there now.
So the next one we want is onRestore
InstanceState, but just before adding
that one I'm going to move the cursor to
the final brace in the code, and notice
that if I do that and now do a
Control O, nothing seems to work.
That's because we're no longer inside
the code in the class. The same thing
would happen if I placed the cursor above
the class declaration, so I'd say on line
11, and if I do Ctrl-O again, and just to
confirm I can go to Code, Generate and notice
that the pop-up menu has changed
completely. So you need to make sure to
get this Generate menu option to pop up,
and for our, the functions that
we need to be able to be generated, we
need to actually be inside the class. So
make sure that the cursor is actually
placed inside the class somewhere. Al
right, so I'm going to move the cursor,
though, as you're seeing it now, below the on
Start method. I'm now going to do a Ctrl-
O, notice that that pops up the screen
straightaway now. Then I'm going to type
onRestore, on R e s t o r e, and you can
see that there's two options that have
popped up there. And you can see that, in
my case, it's actually selected the right
one but notice there's two here. So we've got
onRestoreInstanceState and there's also this
second one, that's got a second argument
there, and if we scroll over we can see a
bit more about that, persistentState,
etc. So the one we actually want
is the one that's got a single bundle
argument, so in other words, it's this
previous one here that I've now pressed
the up arrow key to access to, to get
access to. We don't want the one with two
arguments, and that's because if you
choose that option, things won't work. And
the same thing's going to happen when we
get to onSaveInstanceState, so again
be careful and choose the one that takes
a single argument, and I'm going to
select onRestoreInstanceState.
Now you do want to be careful with
things like onResume as well, as there's
also an onResume Fragment and onPost
Resume. We don't want either of those.
The point is to make sure you always
select the one that takes a single
argument. Alright so your
mini-challenge: using ctrl-O, add
the method calls for the remaining
methods that we'll need, and remember to
include the onRestart method that's off
the top right here. Now I'm going to put
the last slide back here so you can see
what they're called. Pause the video
and generate the remaining methods and
I'll see you when you get back.
Alright, so hopefully you managed to do
that. What I'm going to do now is add the
remaining methods so we can actually,
ultimately, add some debug code to them.
So I'm going to do my Ctrl-O, first
one's going to be onResume, onResume,
noticing that's onPostResume, down
arrow and get to the onResume. Next one
I want, I'll make a bit of space here;
onPause, onSaveInstanceState. Now
notice with that one, that's the one with
the two arguments. I'm going to do the down arrow
to get to the one that's got the single
argument, onStop, and I'm just using the up
and down arrow keys to navigate to the
right one. We want two more now; on
Restart, the correct one, and finally
we want onDestroy. Okay, so that should
be all of them. So that's pretty cool, as
you can see, that Android Studio is
generating a lot of that code for us, and
doing it this way means that we won't
have any errors in the methods. These
stubs, which is another word for empty
methods, have been created for us with
the correct parameters, and the call to
super that we need as well. At this point,
we're now ready to add our own code to
get these functions to perform some
other tasks. Now we're going to be
looking at doing something useful in a
minute. We'll save the text of the
TextView, so that it's not lost when the
device is rotated. But for now though,
we're just going to get, we're going
to log the fact that these functions are
being called, so we can see what's going
on. Now if we go back and have a look at
the onCreate method, we've already got
the code, the basic logging line in
that method. So to save typing, what I'm
going to do is copy and paste that, copy
that line. And I'm gonna scroll on
down now to the onStart method, and I'm
going to paste that before the call to
super.onStart. Alright, and paste that
in there now. Now this next
bit's very important because things
will be extremely confusing if you get
it wrong. So we've copied that line into
the onStart function, so it's very
important now to modify the text,
to correctly reflect the function that's
being called. Now if you leave it as
onCreate, then the log entries won't make
sense. So double-click the function there.
I'm going to double click onStart here.
I'm going to copy that,
Ctrl-C on Windows or Linux, or command
C, in my case, on a Mac. Then I'm going to
double click the onCreate and then do a
command V on a Mac, which of course would
be Ctrl-V on Linux or Windows, and you
can see that I've replaced that now with
the right name. So you'll be copying that
line into all the lifecycle methods, so
make sure you update the function name
each time. Did I say you again here? Well
that sounds like another mini-challenge.
Just as a useful tip here, if
you're using the mouse left-handed, you
can also do control insert to copy and
shift insert to paste on Windows or
Linux. That means that you can copy and
paste with your right hand, and don't have
to keep moving from the mouse to the
keyboard. Alright, so the mini-challenge,
you've probably guessed what this
mini-challenge is going to be: just to
add the logging lines for the remaining
life cycle methods.
Alright, so pause the video. See if you
can actually add all that to the various
life cycle methods and I'll see you when
you get back. Pause the video now.
Alright, so hopefully you managed to sort
that out. Let's so go ahead and change
the rest of these now, before we
finish the video. So I'm going to copy
the onStart method. I'm going to put
each one before the super call, using the
mechanism that we talked about. I'm going
to copy the function name and paste it.
Copy the line again, onResume, onPause.
Next one, onSaveInstanceState, onStop.
Two more to go, onRestart. Then
the last one will be onDestroy. Alright,
so that should be all of them. So before
finishing, just check each function to
make sure the correct function name is
being logged. But I'll stop the video
here. We'll run the app in the next video
and we'll have a good look at what's
happening to our activity when various
things happen. So I'll see you in the next
video.