In the previous video we added some
debug logging to our app. Now in this
video, we're gonna have a look at what it
does and how it can be useful. Now at the
bottom of the Android Studio screen, are a
couple of panes that can be really
useful when you get around to running
your apps. Now the first one, down here, is
the run window. So run the, what I'm going
to do is run the app now, and just confirm
that we can see that running. You can see
that running now. Now I'm going to click
on the run tab over here, to actually see
some information about it, and it starts
off showing the adb commands that
Android Studio uses to install and run
the device. Now we're going to be looking
at adb, which is the Android debug bridge,
a lot later in the course.
But if you get problems running your
apps, then we may ask you to have a look
in here and paste any error messages
into the discussion in Q&A area.
Now hopefully, Android Studio will
connect your emulator or device. If
you've got a slow system with less than,
say, 8 gigabytes of RAM, then it's a good
idea to start your emulator manually. And
you can see that in videos that I've
been doing, in some cases I've been
running the emulator, or having the emulator
already running. And by the way, you can
do that from the AVD manager that we saw
in section 3. Now starting the emulator
before you start coding wll give it
time to start up, and saves you having to
wait when you run your app. Ok, so that's
the Run tab. The second tab is used for
once your app actually runs, and I'm
ignoring the TODO at this stage. And it
used to be called the Android monitor
pane, but in Android Studio 3 it's now been
renamed to logcat, and that's this is one here,
and possibly they renamed it because
that's what most people called it anyway.
Now the logcat, as I click that, that
shows all sorts of things that are
happening on any connected device or
emulator, and with a real device you'd
see loads of entries appearing in here.
And you'd probably also see some warnings
and probably some errors as well, and
most of these will have nothing to do
with your app. So the trick here is to
work out what's what, because there can
be a lot of output in here, but
fortunately we can filter that. Now over
to the far right here, is a drop down and
at the moment, you can see that it's been
set to Show
only selected application. Now you might
think that that's enough and we don't
need to provide a filter as well, but it
also shows all sorts of system events.
Now if we want a full picture of what's
being logged, we can change this drop
down to No filters, as I've done there now.
Now we get a ton of output as I scroll
up. You can see there's a lot of
information there, and that's with the
emulator just sitting there, not really
doing much. And all of that might be
useful to Google engineers but I think,
at this point, it's fairly confusing to
us. So what I'm going to do is put that
back to show only the selected
application again, Show any selected
application. Now if you're debugging more
than one app at once,
maybe you've created a GUI app and a
service, for example, then you select
which one to display in the second
drop-down, over here, the second from
the left,
and you could choose which particular
app that you're wanting to get the
output for. You can see that there's
quite a few Android apps that are
actually running, right now on this
emulator, but you can see that's ours
there, obviously, by the name. Alright, so
I'm going to leave that on buttoncounter
app still, which is our app, and we can
look at what happened when we launched the
app. Now if we scroll back to the top of
the entries, and if you scroll down and
have a look, we should be able to see the
onCreate method message that we actually
put into our code, and there's the on
Create called message. So that's the text
we used in the logd call, or at least
the last part is, the onCcreate called.
The rest has been added automatically by
Android to the left-hand side. Alright, so
there's still a lot of other stuff, though,
you can see here that we're not really
interested in at this stage. So we can
tidy up this log a little bit more. So
there's a search box towards the
right-hand side over here. So what we can
do, is we can further filter what's being
displayed, and that's one of the reasons
for providing the TAG that we did in the
previous video, in our login call. Our TAG
was MainActivity, so I'm going to type
MainActivity into the box, mainactivity,
and you can see that we've got on
Create called now, which was the message. Now
I suggest always typing in lowercase
here. By all means experiment, but
generally, you'll get
better results with lower case. Basically,
lower case will always work, but if you
try to match the case of the TAG and
make a mistake, then it doesn't work. So
I'm going to quickly demonstrate that by
deleting the contents of the box, then
typing Main, this time with a capital M.
So that's obviously working fine, but if
I then start to add activity, but using a
lowercase a, a c, you can see that the
log output has disappeared. In other
words, nothing matches so all the log
entries disappear. And once again,
experiment after you watch this and the
next video. Alright, so I'm going to put
that mainactivity back in lower case
again, and we only get the log entry
entries from our MainActivity class.
Now at the moment there's only one
entry, onCreate called, and we will be
adding some more, but at the moment I
just want to get you familiar with
filtering the logcat. Now if you recall,
we also added login to the buttons
onClickListener. So I'm going to switch
over to the emulator now. Type in some
text and click on the button, and you can see
what appeared in the logcat pane.
Basically, every time the button's tapped
we're going to get that second message,
and we can just go back and just click it a
few more times.
You can see it's being outputted into
the logcat pane every time that button's
actually tapped. So that's pretty cool
and it's a very useful way to see when
you miss it, when your methods get called.
Now I've been telling you that the
activity will be destroyed and recreated
when the device is rotated. Now if that's the
case, we should see onCreate being called
again when we switch to landscape. Now
before I do that, there's something else
I want to show you. And it's not obvious
and it's also not documented, at least as
far as we've been able to find. Now we
can quickly end up with a lot of log
entries, even with the filters that we've
set up, and that can make working out
what's just happened quite difficult. Now
fortunately, we can break up these log
entries a bit. What we're about to do is
rotate the device into landscape, so we
can add a comment to that effect in the
logcat. Now if we move, or
click the mouse after the last entry in
the logcat, we actually get a line break,
as you can see there, the cursor moved
down. And if we type in something
here, so rotating into landscape, and
press ENTER again. And now we're going to
go back and rotate the device into
landscape, and if I do that,
you can see what's happened, that
obviously, the device has been rotated
into landscape mode. And as expected,
we can see the onCreate being called
again, and now that we've actually typed
in that message, it's very easy to see
which methods were called after we did
that. So if we then come back here
and then add another message, back to
portrait, then go back to the device, our
emulator, then go back to portrait, we can
see that our logcat is telling us that
onCreate's called again, and also now the
layout of the log is probably a lot more
intuitive. Now this is going to be far
more informative when we log the rest of
the lifecycle methods, but already you
can see that it's a great way to see
what's going on with our apps, and which
of our methods the Android framework is
calling. Now I said that this ability to
type comments into the logcat is
undocumented. It's been working for the
last few releases of Android Studio but
that doesn't mean it's going to
continue to work. And the reason for that
is Google changes things all the time. So
make use of it while as long
as it lasts, or as long as it lasts, and
if you find that you can no longer type
into the logcat, you'll just have to accept
that the facility's been removed, for
whatever reason. Now the last time I
mentioned an undocumented feature in
a video, Google removed it two weeks
later. So I'm just, so that's why I'm
telling you now that it might stop
working. Alright, so that's the logcat
and how to use it.
Now I haven't mentioned this first
drop-down box, and if you've got several
emulators or devices connected, it lets
you select which one you want to view
the logs for. Keep an eye on that because
if you close down the emulator and
restart it, you might end up with one
marked as dead. Now obviously, it won't
log anything if it's not running, so use
that drop box, if that's the case, to
choose the one that is running. Alright,
I'm gonna stop the video here. In the
next video we'll add logging to the
other life cycle methods, and see exactly
what happens to our app while it's
running. So I'll see you in the next
video.