1
00:00:04,750 --> 00:00:07,359
Okay, so in the last video we saw how to

2
00:00:07,359 --> 00:00:09,969
use the Android Studio logcat, and how

3
00:00:09,969 --> 00:00:12,459
useful it is to see a log entry when

4
00:00:12,459 --> 00:00:14,289
your functions are called. So now we're

5
00:00:14,289 --> 00:00:15,910
going to add logging to all the

6
00:00:15,910 --> 00:00:18,189
lifecycle functions. Doing that will let

7
00:00:18,189 --> 00:00:20,289
us see exactly what happens when we do

8
00:00:20,289 --> 00:00:22,630
various 'Androidy' things while our app's

9
00:00:22,630 --> 00:00:24,579
running. We'll put the

10
00:00:24,579 --> 00:00:26,890
screen to sleep, for example, and also

11
00:00:26,890 --> 00:00:29,109
switch to another application while ours

12
00:00:29,109 --> 00:00:30,849
is running. Now there's quite a few

13
00:00:30,849 --> 00:00:32,980
functions that we need to add, but

14
00:00:32,980 --> 00:00:34,780
fortunately we don't have to do a lot of

15
00:00:34,780 --> 00:00:37,090
typing, and that's because Android Studio

16
00:00:37,090 --> 00:00:39,610
can generate the basic functions for us.

17
00:00:39,610 --> 00:00:41,890
So I'm going to start by creating a new

18
00:00:41,890 --> 00:00:44,770
line here, after the onCreate method

19
00:00:44,770 --> 00:00:46,420
because that's where our code's going to

20
00:00:46,420 --> 00:00:51,040
go. And what we're going to do then, is

21
00:00:51,040 --> 00:00:53,860
come up here and select the Code menu,

22
00:00:53,860 --> 00:00:55,750
or click the Code menu, and there's this

23
00:00:55,750 --> 00:00:58,240
optional over here for Generate, which is

24
00:00:58,240 --> 00:01:00,790
a code generation. And note the shortcut

25
00:01:00,790 --> 00:01:04,328
on a Mac is command n and alt insert on

26
00:01:04,328 --> 00:01:05,920
a PC so you can access it that way in

27
00:01:05,920 --> 00:01:07,540
the future. So I'm going to click on that

28
00:01:07,540 --> 00:01:09,520
now, and that opens up a little popup

29
00:01:09,520 --> 00:01:11,560
menu down here, and you can see that

30
00:01:11,560 --> 00:01:13,659
there's various options on code that

31
00:01:13,659 --> 00:01:15,670
we can get Android Studio to generate

32
00:01:15,670 --> 00:01:17,950
for us. The one we want at this time is

33
00:01:17,950 --> 00:01:20,590
Override Methods, and that's probably the

34
00:01:20,590 --> 00:01:23,080
the one that you'll be using

35
00:01:23,080 --> 00:01:26,050
most often. You can see that Override and

36
00:01:26,050 --> 00:01:27,130
Implement have actually got their own

37
00:01:27,130 --> 00:01:29,170
keyboard shortcuts, if you want to get to

38
00:01:29,170 --> 00:01:31,240
those in the future. Now I'm going

39
00:01:31,240 --> 00:01:33,820
to select override methods, and that

40
00:01:33,820 --> 00:01:36,370
actually brings up a huge list. Alright,

41
00:01:36,370 --> 00:01:38,860
so the first one that I want would be on

42
00:01:38,860 --> 00:01:41,020
Destroy, so I could come all the way down

43
00:01:41,020 --> 00:01:43,180
and try and find that. So we can scroll

44
00:01:43,180 --> 00:01:47,659
down to find that method.

45
00:01:47,659 --> 00:01:49,979
There it is there, so I can actually click on

46
00:01:49,979 --> 00:01:52,140
that and then I could hold the ctrl key

47
00:01:52,140 --> 00:01:53,400
down and scroll through the list,

48
00:01:53,400 --> 00:01:55,590
selecting all the ones I wanted. But that

49
00:01:55,590 --> 00:01:57,690
can get tedious, searching manually, and it

50
00:01:57,690 --> 00:01:59,610
also inserts the methods in whatever

51
00:01:59,610 --> 00:02:01,890
order it feels like. Now, although the

52
00:02:01,890 --> 00:02:03,750
order that they appear in the code

53
00:02:03,750 --> 00:02:05,490
doesn't matter at all, as far as the

54
00:02:05,490 --> 00:02:07,860
computer's concerned, I'd like them to

55
00:02:07,860 --> 00:02:09,630
appear in the order that they're called,

56
00:02:09,630 --> 00:02:11,520
just so that we get used to that order.

57
00:02:11,520 --> 00:02:14,130
So luckily, we can actually start typing

58
00:02:14,130 --> 00:02:16,500
a method and Android Studio will find

59
00:02:16,500 --> 00:02:19,010
it in the list. So I can start typing on

60
00:02:19,010 --> 00:02:23,310
Start, and I can then use the down

61
00:02:23,310 --> 00:02:24,959
arrow to jump to the next matching

62
00:02:24,959 --> 00:02:26,730
method. So I can click on the down arrow

63
00:02:26,730 --> 00:02:28,019
and you can see it's now on the right

64
00:02:28,019 --> 00:02:30,480
method. So once I've done that, I can then

65
00:02:30,480 --> 00:02:32,730
press ENTER to insert the onStart

66
00:02:32,730 --> 00:02:35,370
method, so I'm going to do that. You can see the

67
00:02:35,370 --> 00:02:36,959
onStart method is showing there now.

68
00:02:36,959 --> 00:02:39,569
So the next one we want is onRestore

69
00:02:39,569 --> 00:02:41,670
InstanceState, but just before adding

70
00:02:41,670 --> 00:02:42,989
that one I'm going to move the cursor to

71
00:02:42,989 --> 00:02:47,220
the final brace in the code, and notice

72
00:02:47,220 --> 00:02:48,930
that if I do that and now do a

73
00:02:48,930 --> 00:02:52,130
Control O, nothing seems to work.

74
00:02:52,130 --> 00:02:54,360
That's because we're no longer inside

75
00:02:54,360 --> 00:02:56,430
the code in the class. The same thing

76
00:02:56,430 --> 00:02:57,900
would happen if I placed the cursor above

77
00:02:57,900 --> 00:03:00,750
the class declaration, so I'd say on line

78
00:03:00,750 --> 00:03:04,799
11, and if I do Ctrl-O again, and just to

79
00:03:04,799 --> 00:03:07,980
confirm I can go to Code, Generate and notice

80
00:03:07,980 --> 00:03:09,840
that the pop-up menu has changed

81
00:03:09,840 --> 00:03:12,780
completely. So you need to make sure to

82
00:03:12,780 --> 00:03:15,269
get this Generate menu option to pop up,

83
00:03:15,269 --> 00:03:17,220
and for our, the functions that

84
00:03:17,220 --> 00:03:19,200
we need to be able to be generated, we

85
00:03:19,200 --> 00:03:21,780
need to actually be inside the class. So

86
00:03:21,780 --> 00:03:23,070
make sure that the cursor is actually

87
00:03:23,070 --> 00:03:25,230
placed inside the class somewhere. Al

88
00:03:25,230 --> 00:03:26,040
right, so I'm going to move the cursor,

89
00:03:26,040 --> 00:03:29,160
though, as you're seeing it now, below the on

90
00:03:29,160 --> 00:03:31,170
Start method. I'm now going to do a Ctrl-

91
00:03:31,170 --> 00:03:35,280
O, notice that that pops up the screen

92
00:03:35,280 --> 00:03:37,320
straightaway now. Then I'm going to type

93
00:03:37,320 --> 00:03:41,880
onRestore, on R e s t o r e, and you can

94
00:03:41,880 --> 00:03:43,049
see that there's two options that have

95
00:03:43,049 --> 00:03:44,970
popped up there. And you can see that, in

96
00:03:44,970 --> 00:03:46,470
my case, it's actually selected the right

97
00:03:46,470 --> 00:03:48,989
one but notice there's two here. So we've got

98
00:03:48,989 --> 00:03:50,730
onRestoreInstanceState and there's also this

99
00:03:50,730 --> 00:03:52,709
second one, that's got a second argument

100
00:03:52,709 --> 00:03:55,019
there, and if we scroll over we can see a

101
00:03:55,019 --> 00:03:56,459
bit more about that, persistentState,

102
00:03:56,459 --> 00:03:59,260
etc. So the one we actually want

103
00:03:59,260 --> 00:04:01,239
is the one that's got a single bundle

104
00:04:01,239 --> 00:04:02,530
argument, so in other words, it's this

105
00:04:02,530 --> 00:04:04,030
previous one here that I've now pressed

106
00:04:04,030 --> 00:04:06,549
the up arrow key to access to, to get

107
00:04:06,549 --> 00:04:08,560
access to. We don't want the one with two

108
00:04:08,560 --> 00:04:10,269
arguments, and that's because if you

109
00:04:10,269 --> 00:04:12,610
choose that option, things won't work. And

110
00:04:12,610 --> 00:04:14,439
the same thing's going to happen when we

111
00:04:14,439 --> 00:04:16,660
get to onSaveInstanceState, so again

112
00:04:16,660 --> 00:04:18,399
be careful and choose the one that takes

113
00:04:18,399 --> 00:04:20,108
a single argument, and I'm going to

114
00:04:20,108 --> 00:04:22,810
select onRestoreInstanceState. 

115
00:04:22,810 --> 00:04:24,550
Now you do want to be careful with

116
00:04:24,550 --> 00:04:26,710
things like onResume as well, as there's

117
00:04:26,710 --> 00:04:28,990
also an onResume Fragment and onPost

118
00:04:28,990 --> 00:04:30,639
Resume. We don't want either of those.

119
00:04:30,639 --> 00:04:32,169
The point is to make sure you always

120
00:04:32,169 --> 00:04:34,060
select the one that takes a single

121
00:04:34,060 --> 00:04:36,490
argument. Alright so your

122
00:04:36,490 --> 00:04:39,070
mini-challenge: using ctrl-O, add

123
00:04:39,070 --> 00:04:41,080
the method calls for the remaining

124
00:04:41,080 --> 00:04:43,630
methods that we'll need, and remember to

125
00:04:43,630 --> 00:04:45,699
include the onRestart method that's off

126
00:04:45,699 --> 00:04:48,010
the top right here. Now I'm going to put

127
00:04:48,010 --> 00:04:49,690
the last slide back here so you can see

128
00:04:49,690 --> 00:04:51,280
what they're called. Pause the video

129
00:04:51,280 --> 00:04:53,020
and generate the remaining methods and

130
00:04:53,020 --> 00:04:55,770
I'll see you when you get back.

131
00:04:55,770 --> 00:04:57,870
Alright, so hopefully you managed to do

132
00:04:57,870 --> 00:04:59,400
that. What I'm going to do now is add the

133
00:04:59,400 --> 00:05:01,880
remaining methods so we can actually,

134
00:05:01,880 --> 00:05:04,410
ultimately, add some debug code to them.

135
00:05:04,410 --> 00:05:06,479
So I'm going to do my Ctrl-O, first

136
00:05:06,479 --> 00:05:09,080
one's going to be onResume, onResume,

137
00:05:09,080 --> 00:05:11,490
noticing that's onPostResume, down

138
00:05:11,490 --> 00:05:15,960
arrow and get to the onResume. Next one

139
00:05:15,960 --> 00:05:17,759
I want, I'll make a bit of space here;

140
00:05:17,759 --> 00:05:31,380
onPause, onSaveInstanceState. Now

141
00:05:31,380 --> 00:05:32,759
notice with that one, that's the one with

142
00:05:32,759 --> 00:05:34,560
the two arguments. I'm going to do the down arrow

143
00:05:34,560 --> 00:05:35,880
to get to the one that's got the single

144
00:05:35,880 --> 00:05:44,849
argument, onStop, and I'm just using the up

145
00:05:44,849 --> 00:05:46,259
and down arrow keys to navigate to the

146
00:05:46,259 --> 00:05:47,759
right one. We want two more now; on

147
00:05:47,759 --> 00:05:53,099
Restart, the correct one, and finally

148
00:05:53,099 --> 00:05:56,819
we want onDestroy. Okay, so that should

149
00:05:56,819 --> 00:05:58,590
be all of them. So that's pretty cool, as

150
00:05:58,590 --> 00:06:00,210
you can see, that Android Studio is

151
00:06:00,210 --> 00:06:02,280
generating a lot of that code for us, and

152
00:06:02,280 --> 00:06:04,080
doing it this way means that we won't

153
00:06:04,080 --> 00:06:05,940
have any errors in the methods. These

154
00:06:05,940 --> 00:06:07,680
stubs, which is another word for empty

155
00:06:07,680 --> 00:06:10,080
methods, have been created for us with

156
00:06:10,080 --> 00:06:12,330
the correct parameters, and the call to

157
00:06:12,330 --> 00:06:14,849
super that we need as well. At this point,

158
00:06:14,849 --> 00:06:16,800
we're now ready to add our own code to

159
00:06:16,800 --> 00:06:18,270
get these functions to perform some

160
00:06:18,270 --> 00:06:20,099
other tasks. Now we're going to be

161
00:06:20,099 --> 00:06:21,840
looking at doing something useful in a

162
00:06:21,840 --> 00:06:23,580
minute. We'll save the text of the

163
00:06:23,580 --> 00:06:25,440
TextView, so that it's not lost when the

164
00:06:25,440 --> 00:06:27,630
device is rotated. But for now though,

165
00:06:27,630 --> 00:06:29,039
we're just going to get, we're going

166
00:06:29,039 --> 00:06:30,389
to log the fact that these functions are

167
00:06:30,389 --> 00:06:32,490
being called, so we can see what's going

168
00:06:32,490 --> 00:06:35,009
on. Now if we go back and have a look at

169
00:06:35,009 --> 00:06:38,639
the onCreate method, we've already got

170
00:06:38,639 --> 00:06:41,250
the code, the basic logging line in

171
00:06:41,250 --> 00:06:43,469
that method. So to save typing, what I'm

172
00:06:43,469 --> 00:06:47,550
going to do is copy and paste that, copy

173
00:06:47,550 --> 00:06:50,159
that line. And I'm gonna scroll on

174
00:06:50,159 --> 00:06:51,690
down now to the onStart method, and I'm

175
00:06:51,690 --> 00:06:53,969
going to paste that before the call to

176
00:06:53,969 --> 00:06:57,509
super.onStart. Alright, and paste that

177
00:06:57,509 --> 00:06:59,550
in there now. Now this next

178
00:06:59,550 --> 00:07:01,080
bit's very important because things

179
00:07:01,080 --> 00:07:02,669
will be extremely confusing if you get

180
00:07:02,669 --> 00:07:05,400
it wrong. So we've copied that line into

181
00:07:05,400 --> 00:07:07,560
the onStart function, so it's very

182
00:07:07,560 --> 00:07:09,490
important now to modify the text,

183
00:07:09,490 --> 00:07:11,440
to correctly reflect the function that's

184
00:07:11,440 --> 00:07:13,360
being called. Now if you leave it as

185
00:07:13,360 --> 00:07:15,760
onCreate, then the log entries won't make

186
00:07:15,760 --> 00:07:18,100
sense. So double-click the function there.

187
00:07:18,100 --> 00:07:19,600
I'm going to double click onStart here.

188
00:07:19,600 --> 00:07:21,430
I'm going to copy that,

189
00:07:21,430 --> 00:07:23,800
Ctrl-C on Windows or Linux, or command

190
00:07:23,800 --> 00:07:25,660
C, in my case, on a Mac. Then I'm going to

191
00:07:25,660 --> 00:07:29,110
double click the onCreate and then do a

192
00:07:29,110 --> 00:07:31,540
command V on a Mac, which of course would

193
00:07:31,540 --> 00:07:33,970
be Ctrl-V on Linux or Windows, and you

194
00:07:33,970 --> 00:07:35,350
can see that I've replaced that now with

195
00:07:35,350 --> 00:07:37,840
the right name. So you'll be copying that

196
00:07:37,840 --> 00:07:40,150
line into all the lifecycle methods, so

197
00:07:40,150 --> 00:07:41,620
make sure you update the function name

198
00:07:41,620 --> 00:07:44,320
each time. Did I say you again here? Well

199
00:07:44,320 --> 00:07:45,640
that sounds like another mini-challenge.

200
00:07:45,640 --> 00:07:47,650
Just as a useful tip here, if

201
00:07:47,650 --> 00:07:49,540
you're using the mouse left-handed, you

202
00:07:49,540 --> 00:07:51,550
can also do control insert to copy and

203
00:07:51,550 --> 00:07:53,200
shift insert to paste on Windows or

204
00:07:53,200 --> 00:07:55,060
Linux. That means that you can copy and

205
00:07:55,060 --> 00:07:56,710
paste with your right hand, and don't have

206
00:07:56,710 --> 00:07:57,820
to keep moving from the mouse to the

207
00:07:57,820 --> 00:07:59,890
keyboard. Alright, so the mini-challenge,

208
00:07:59,890 --> 00:08:01,180
you've probably guessed what this

209
00:08:01,180 --> 00:08:02,919
mini-challenge is going to be: just to

210
00:08:02,919 --> 00:08:04,690
add the logging lines for the remaining

211
00:08:04,690 --> 00:08:05,740
life cycle methods.

212
00:08:05,740 --> 00:08:08,230
Alright, so pause the video. See if you

213
00:08:08,230 --> 00:08:10,290
can actually add all that to the various

214
00:08:10,290 --> 00:08:12,730
life cycle methods and I'll see you when

215
00:08:12,730 --> 00:08:15,279
you get back. Pause the video now.

216
00:08:15,279 --> 00:08:17,409
Alright, so hopefully you managed to sort

217
00:08:17,409 --> 00:08:19,599
that out. Let's so go ahead and change

218
00:08:19,599 --> 00:08:21,369
the rest of these now, before we

219
00:08:21,369 --> 00:08:23,339
finish the video. So I'm going to copy

220
00:08:23,339 --> 00:08:25,629
the onStart method. I'm going to put

221
00:08:25,629 --> 00:08:28,869
each one before the super call, using the

222
00:08:28,869 --> 00:08:30,909
mechanism that we talked about. I'm going

223
00:08:30,909 --> 00:08:33,610
to copy the function name and paste it.

224
00:08:33,610 --> 00:08:51,000
Copy the line again, onResume, onPause.

225
00:08:51,000 --> 00:09:04,259
Next one, onSaveInstanceState, onStop.

226
00:09:04,259 --> 00:09:10,839
Two more to go, onRestart. Then

227
00:09:10,839 --> 00:09:18,069
the last one will be onDestroy. Alright,

228
00:09:18,069 --> 00:09:19,959
so that should be all of them. So before

229
00:09:19,959 --> 00:09:21,430
finishing, just check each function to

230
00:09:21,430 --> 00:09:23,019
make sure the correct function name is

231
00:09:23,019 --> 00:09:25,449
being logged. But I'll stop the video

232
00:09:25,449 --> 00:09:27,490
here. We'll run the app in the next video

233
00:09:27,490 --> 00:09:28,600
and we'll have a good look at what's

234
00:09:28,600 --> 00:09:30,819
happening to our activity when various

235
00:09:30,819 --> 00:09:32,680
things happen. So I'll see you in the next

236
00:09:32,680 --> 00:09:34,949
video.

