1
00:00:05,669 --> 00:00:07,659
Alright, in the previous video,

2
00:00:07,659 --> 00:00:09,077
or actually the one before that,

3
00:00:09,077 --> 00:00:10,899
we saw some strange behaviour

4
00:00:10,899 --> 00:00:13,766
when the emulator was rotated into landscape.

5
00:00:13,766 --> 00:00:16,740
And the contents of the TextView were cleared,

6
00:00:16,740 --> 00:00:19,686
which was expected, because Android destroys the activity

7
00:00:19,686 --> 00:00:22,936
when things like a change in orientation happen.

8
00:00:22,936 --> 00:00:26,186
What I didn't explain though, was how come the editText

9
00:00:26,186 --> 00:00:27,874
retained it's contents.

10
00:00:27,874 --> 00:00:30,314
Not sure whether you picked that up, so let's just run that again,

11
00:00:30,314 --> 00:00:33,541
and I'll show you what I mean.

12
00:00:33,541 --> 00:00:35,624
(typing)

13
00:00:39,636 --> 00:00:41,497
Alright, so here's our app running,

14
00:00:41,497 --> 00:00:42,993
so I'm gonna enter some text in here now,

15
00:00:42,993 --> 00:00:44,095
in the editText.

16
00:00:44,095 --> 00:00:45,678
So "Tim was here",

17
00:00:47,312 --> 00:00:49,302
tap the button, and it's expected,

18
00:00:49,302 --> 00:00:50,335
or even though I made a typo,

19
00:00:50,335 --> 00:00:53,351
"Tim was her", 'Tim was here' was meant to be typed.

20
00:00:53,351 --> 00:00:55,830
The point is that it actually worked.

21
00:00:55,830 --> 00:00:58,312
And what I'm going to do is enter something again,

22
00:00:58,312 --> 00:01:01,012
(typing) "Tim was still",

23
00:01:01,012 --> 00:01:03,705
this time I'll type the word corrently, "here".

24
00:01:03,705 --> 00:01:05,428
Alright, now without tapping the button,

25
00:01:05,428 --> 00:01:07,108
we're going to go into landscape mode again,

26
00:01:07,108 --> 00:01:11,669
so I'm going to move that over and go into landscape.

27
00:01:11,669 --> 00:01:14,919
And we'll just close the keyboard down.

28
00:01:15,957 --> 00:01:18,094
And notice that the TextView has been

29
00:01:18,094 --> 00:01:19,443
cleared of its contents,

30
00:01:19,443 --> 00:01:20,534
but the text that I've typed

31
00:01:20,534 --> 00:01:22,774
into the editText is still there.

32
00:01:22,774 --> 00:01:23,976
So, how does that work?

33
00:01:23,976 --> 00:01:25,679
What's actually going on?

34
00:01:25,679 --> 00:01:28,346
It's as though something is Saving State

35
00:01:28,346 --> 00:01:30,646
of our Activity Instance.

36
00:01:30,646 --> 00:01:33,578
No, I haven't developed a speech impediment.

37
00:01:33,578 --> 00:01:36,017
There's a reason I was stressing those three words;

38
00:01:36,017 --> 00:01:38,682
Save, Instance, and State,

39
00:01:38,682 --> 00:01:41,484
to give you a clue as to what might be involved.

40
00:01:41,484 --> 00:01:43,125
Now if you've guessed that it's got something to do

41
00:01:43,125 --> 00:01:46,235
with the Bundle in the onCreate method, over here,

42
00:01:46,235 --> 00:01:48,426
savedInstantState on line 14,

43
00:01:48,426 --> 00:01:51,300
then you're actually right.

44
00:01:51,300 --> 00:01:53,892
Now, a Bundle is an object that can be used to

45
00:01:53,892 --> 00:01:57,020
pass data around, within the Android framework.

46
00:01:57,020 --> 00:01:59,270
Now it's not the only object that can be used for that,

47
00:01:59,270 --> 00:02:02,830
but it's the one being used here.

48
00:02:02,830 --> 00:02:06,275
Now when our Activity starts, the onCreate method is

49
00:02:06,275 --> 00:02:09,138
called automatically by the Android framework.

50
00:02:09,138 --> 00:02:11,512
Now when it's called, it's given a Bundle

51
00:02:11,512 --> 00:02:14,082
containing all the data needed to restore it to the state

52
00:02:14,082 --> 00:02:16,684
it was in, when it was destroyed.

53
00:02:16,684 --> 00:02:19,712
Now when Android destroys the activity as a result of

54
00:02:19,712 --> 00:02:22,130
rotating the device or for a number of other reasons,

55
00:02:22,130 --> 00:02:25,718
the Instant State of the activity is automatically saved,

56
00:02:25,718 --> 00:02:28,216
before the activity is destroyed.

57
00:02:28,216 --> 00:02:31,276
Now all of that is taken care of by the activity

58
00:02:31,276 --> 00:02:33,338
or AppCompactActivity classes,

59
00:02:33,338 --> 00:02:34,884
so because our MainActivity

60
00:02:34,884 --> 00:02:36,700
extends AppCompactActivity,

61
00:02:36,700 --> 00:02:39,835
we get the advantage of that behaviour.

62
00:02:39,835 --> 00:02:42,145
The contents of the editText are saved for us,

63
00:02:42,145 --> 00:02:44,683
because the contents of any editable widgets form

64
00:02:44,683 --> 00:02:47,275
part of the activities Instant Dtate.

65
00:02:47,275 --> 00:02:49,879
That only applies to editable word widgets, though.

66
00:02:49,879 --> 00:02:51,541
The TextView isn't editable,

67
00:02:51,541 --> 00:02:54,261
so therefore the contents aren't saved.

68
00:02:54,261 --> 00:02:56,111
Now, that explains the behaviour

69
00:02:56,111 --> 00:02:57,792
we've just observed with our app,

70
00:02:57,792 --> 00:03:01,585
when I rotated the virtual device into landscape mode.

71
00:03:01,585 --> 00:03:03,500
So if the base class AppCompActivity

72
00:03:03,500 --> 00:03:05,062
contains the functionality

73
00:03:05,062 --> 00:03:07,912
to restore the savedInstanceState

74
00:03:07,912 --> 00:03:09,776
when the activity starts,

75
00:03:09,776 --> 00:03:11,946
that explains why we have to call

76
00:03:11,946 --> 00:03:14,466
it's onCreate method from ours.

77
00:03:14,466 --> 00:03:17,734
Now it might be tempting to remove the super.onCreate

78
00:03:17,734 --> 00:03:22,364
savedInstanceStatecall, that's this call here on line 15,

79
00:03:22,364 --> 00:03:24,002
just to see what happens, so lets do that.

80
00:03:24,002 --> 00:03:26,924
I'm going to comment that out.

81
00:03:26,924 --> 00:03:28,526
Now what we probably would expect to see,

82
00:03:28,526 --> 00:03:29,861
is the app will no longer restore

83
00:03:29,861 --> 00:03:33,123
the state of the editText when the orientation changes.

84
00:03:33,123 --> 00:03:35,361
So what I'm gonna do first is move the orientation

85
00:03:35,361 --> 00:03:37,028
back to what it was.

86
00:03:40,763 --> 00:03:42,226
But now, already, by commenting that out,

87
00:03:42,226 --> 00:03:43,807
I've got this warning appearing;

88
00:03:43,807 --> 00:03:46,494
"Overriding method should call super.onCreate"

89
00:03:46,494 --> 00:03:50,661
which is giving us a hint. But we'll try running,

90
00:03:53,524 --> 00:03:55,934
and go back to the emulator,

91
00:03:55,934 --> 00:03:58,934
and we've got an error, immediately.

92
00:04:02,033 --> 00:04:03,846
Well if you come back over here,

93
00:04:03,846 --> 00:04:05,985
and we come back and click on Logcat,

94
00:04:05,985 --> 00:04:09,042
this little tab down the bottom here,

95
00:04:09,042 --> 00:04:10,785
and scroll up a little bit,

96
00:04:10,785 --> 00:04:13,444
we can see we've actually got an error here.

97
00:04:13,444 --> 00:04:15,413
And the actual error here is

98
00:04:15,413 --> 00:04:18,579
'android.util.SuperNotCalledException'

99
00:04:19,753 --> 00:04:21,952
and basically it tells us that our app

100
00:04:21,952 --> 00:04:24,877
didn't call the super.onCreate method.

101
00:04:24,877 --> 00:04:26,991
So this saving and restoring of state

102
00:04:26,991 --> 00:04:30,311
is so important that the Android framework won't allow

103
00:04:30,311 --> 00:04:33,236
an activity not to call the super method to support it.

104
00:04:33,236 --> 00:04:35,707
And again, we saw evidence of that here,

105
00:04:35,707 --> 00:04:36,964
the fact that it's warning us

106
00:04:36,964 --> 00:04:38,437
just by commenting that line out

107
00:04:38,437 --> 00:04:40,436
that it's going to be a problem there,

108
00:04:40,436 --> 00:04:42,203
and then we tried to run the app,

109
00:04:42,203 --> 00:04:44,192
and it crashed, as you can see there.

110
00:04:44,192 --> 00:04:47,024
The error message here, I think, is very clear about that.

111
00:04:47,024 --> 00:04:51,024
The exception is called SuperNotCalledException,

112
00:04:52,107 --> 00:04:54,539
SuperNotCalledException, over here,

113
00:04:54,539 --> 00:04:56,168
which is pretty descriptive in itself,

114
00:04:56,168 --> 00:04:57,437
and you can see that the actual message

115
00:04:57,437 --> 00:05:00,608
that I spelled out before, talked about our MainActivity

116
00:05:00,608 --> 00:05:03,808
and it didn't call, and how it didn't call through to

117
00:05:03,808 --> 00:05:05,712
the super.onCreate.

118
00:05:05,712 --> 00:05:09,312
And again, I'll just close this logcat down now,

119
00:05:09,312 --> 00:05:11,381
we're getting a red error here,

120
00:05:11,381 --> 00:05:12,723
over here in the gutter,

121
00:05:12,723 --> 00:05:15,509
and we've got the underlining on the onCreate.

122
00:05:15,509 --> 00:05:17,570
So I guess we could have saved time after

123
00:05:17,570 --> 00:05:19,608
seeing that and not proceeding any further,

124
00:05:19,608 --> 00:05:20,622
instead of running the app,

125
00:05:20,622 --> 00:05:22,818
but it is useful to see the exceptions sometimes.

126
00:05:22,818 --> 00:05:24,034
Alright, so lets just go back

127
00:05:24,034 --> 00:05:26,216
and uncomment that again,

128
00:05:26,216 --> 00:05:28,541
so that we remove the error.

129
00:05:28,541 --> 00:05:29,493
Now incidentally,

130
00:05:29,493 --> 00:05:33,152
activities aren't just destroyed when you rotate the screen.

131
00:05:33,152 --> 00:05:34,718
There's a whole set of events

132
00:05:34,718 --> 00:05:36,752
which can result in Android destroying it,

133
00:05:36,752 --> 00:05:39,363
and it doesn't always recreate it for you.

134
00:05:39,363 --> 00:05:41,703
If the system is short of memory, for example,

135
00:05:41,703 --> 00:05:43,925
then the Android framework will kill your app,

136
00:05:43,925 --> 00:05:47,103
if it needs the memory for an app with a higher priority.

137
00:05:47,103 --> 00:05:49,954
Fortunately, that's quite a rare event with modern phones,

138
00:05:49,954 --> 00:05:52,525
as they now have a lot more memory than they used to.

139
00:05:52,525 --> 00:05:54,975
But if you open loads of apps then Android will definitely

140
00:05:54,975 --> 00:05:57,104
kill them if it needs the memory.

141
00:05:57,104 --> 00:05:59,320
Alright, so lets swing over to some slides,

142
00:05:59,320 --> 00:06:01,647
and have a look at the activity life cycle,

143
00:06:01,647 --> 00:06:03,169
as well as the events we can use

144
00:06:03,169 --> 00:06:06,555
in our programmes to handle them.

145
00:06:06,555 --> 00:06:09,390
Alright, so this first slide shows the full

146
00:06:09,390 --> 00:06:11,110
lifetime of an app and

147
00:06:11,110 --> 00:06:13,859
what happens when Android needs more memory,

148
00:06:13,859 --> 00:06:15,699
or the user starts another app

149
00:06:15,699 --> 00:06:18,150
or a new activity within our app.

150
00:06:18,150 --> 00:06:20,910
Now when the activity is started for the first time,

151
00:06:20,910 --> 00:06:23,339
it's onCreate method is called.

152
00:06:23,339 --> 00:06:24,910
Now because this is the first time

153
00:06:24,910 --> 00:06:26,339
the activity is started,

154
00:06:26,339 --> 00:06:28,457
there is no saved state as such.

155
00:06:28,457 --> 00:06:31,499
The savedInstanceState bundle is null,

156
00:06:31,499 --> 00:06:33,987
because nothing needs to be restored.

157
00:06:33,987 --> 00:06:37,617
Now although I've got a call to onRestoreInstanceState

158
00:06:37,617 --> 00:06:40,375
in the diagram, this isn't always called.

159
00:06:40,375 --> 00:06:43,533
In fact, it's only called when Bundle is not null,

160
00:06:43,533 --> 00:06:45,463
so the first time you start this app,

161
00:06:45,463 --> 00:06:47,697
this method will not be called.

162
00:06:47,697 --> 00:06:49,625
There's nothing to restore, so Android doesn't

163
00:06:49,625 --> 00:06:50,844
bother calling it.

164
00:06:50,844 --> 00:06:52,985
And we'll come back to that a little bit later.

165
00:06:52,985 --> 00:06:57,273
So the next methods to be called onStart and onResume.

166
00:06:57,273 --> 00:07:01,335
Now once onStart has been called, the activity is visible

167
00:07:01,335 --> 00:07:03,474
but may not yet be in a state where the user

168
00:07:03,474 --> 00:07:05,875
can interact with it.

169
00:07:05,875 --> 00:07:08,446
This is the visible lifetime of the activity,

170
00:07:08,446 --> 00:07:11,936
and happens between the calls to onStart and onStop.

171
00:07:11,936 --> 00:07:14,038
Now during this part of the lifecycle,

172
00:07:14,038 --> 00:07:17,155
the activity may actually be paused.

173
00:07:17,155 --> 00:07:19,806
Now once onResume has been called, the activity is

174
00:07:19,806 --> 00:07:23,395
in the foreground, and is in a running state.

175
00:07:23,395 --> 00:07:25,315
It's in front of all the other activities

176
00:07:25,315 --> 00:07:27,816
and the user can interact with it quite happily.

177
00:07:27,816 --> 00:07:30,595
This is the foreground lifetime of the activity,

178
00:07:30,595 --> 00:07:35,216
which lasts between the calls to onResume and onPause.

179
00:07:35,216 --> 00:07:38,086
Now there are many events that can result in the activity

180
00:07:38,086 --> 00:07:40,680
going between the running and paused states -

181
00:07:40,680 --> 00:07:43,189
things like the device going to sleep, for example.

182
00:07:43,189 --> 00:07:46,248
Now if you put code in the onPause and onResume functions,

183
00:07:46,248 --> 00:07:48,958
then it shouldn't do a lot.

184
00:07:48,958 --> 00:07:50,224
While the activity's running,

185
00:07:50,224 --> 00:07:52,466
another activity could be brought into the foreground

186
00:07:52,466 --> 00:07:54,148
or the phone could go to sleep,

187
00:07:54,148 --> 00:07:55,689
and you'll see examples of starting

188
00:07:55,689 --> 00:07:57,437
new activities in the course,

189
00:07:57,437 --> 00:08:00,658
when we launch a new activity to perform some actions -

190
00:08:00,658 --> 00:08:03,100
such as editing a contact record.

191
00:08:03,100 --> 00:08:06,479
Now if that happens, the current activity is paused.

192
00:08:06,479 --> 00:08:09,319
The onPause method is called before the current activity

193
00:08:09,319 --> 00:08:10,486
loses control.

194
00:08:11,398 --> 00:08:13,409
Now the activity is still in a happy state,

195
00:08:13,409 --> 00:08:16,079
and there's no need to save the state

196
00:08:16,079 --> 00:08:18,991
and restore it again - it's just no longer in the foreground.

197
00:08:18,991 --> 00:08:21,671
Now if you displayed a dialogue on top of your activity,

198
00:08:21,671 --> 00:08:24,703
you can also put the activity into the paused state,

199
00:08:24,703 --> 00:08:27,263
so onPause will be called.

200
00:08:27,263 --> 00:08:28,743
Now Google recommends that you don't

201
00:08:28,743 --> 00:08:32,282
perform intensive operations in onPause,

202
00:08:32,282 --> 00:08:34,462
and they've changed their recommendation,

203
00:08:34,462 --> 00:08:36,630
They used to state that you had to be very careful

204
00:08:36,630 --> 00:08:38,491
to make sure that your onPause function

205
00:08:38,491 --> 00:08:41,642
completed its tasks very quickly.

206
00:08:41,642 --> 00:08:44,102
Failing to do that could result in the next activity

207
00:08:44,102 --> 00:08:46,600
not receiving the focus in time,

208
00:08:46,600 --> 00:08:47,908
resulting in the user being

209
00:08:47,908 --> 00:08:50,240
unable to answer their phone for example.

210
00:08:50,240 --> 00:08:52,347
Now although things have changed, and your app

211
00:08:52,347 --> 00:08:55,078
won't block any other apps that are trying to start,

212
00:08:55,078 --> 00:08:57,344
you should still make sure that any code

213
00:08:57,344 --> 00:08:59,785
you place in onPause executes quickly.

214
00:08:59,785 --> 00:09:02,076
Updating your remote database is a good example

215
00:09:02,076 --> 00:09:04,185
of something that you should not do in here.

216
00:09:04,185 --> 00:09:05,943
You'd wanna perform things like remote updates

217
00:09:05,943 --> 00:09:08,526
in the onStop function instead.

218
00:09:09,385 --> 00:09:12,076
Now what happens next, after onPause is called,

219
00:09:12,076 --> 00:09:14,476
depends on what goes on with the system.

220
00:09:14,476 --> 00:09:17,415
Now if the user brings the activity back to the foreground,

221
00:09:17,415 --> 00:09:19,495
by dismissing a dialogue, for example,

222
00:09:19,495 --> 00:09:21,511
then onResume is called.

223
00:09:21,511 --> 00:09:23,121
If the system needs more memory

224
00:09:23,121 --> 00:09:26,640
and decides to kill the app, then onStop will be called.

225
00:09:26,640 --> 00:09:28,253
That's represented by the arrows on the

226
00:09:28,253 --> 00:09:30,453
right hand side of the slide.

227
00:09:30,453 --> 00:09:32,199
Now if the user presses the back button

228
00:09:32,199 --> 00:09:33,401
to close your activity,

229
00:09:33,401 --> 00:09:36,209
then the activity is destroyed completely.

230
00:09:36,209 --> 00:09:38,670
The only trace of it then is in the Recent Apps,

231
00:09:38,670 --> 00:09:40,533
but it's no longer running at all

232
00:09:40,533 --> 00:09:43,676
and that's the end of the lifecycle, or of it's lifecycle.

233
00:09:43,676 --> 00:09:45,460
If the user launches it again,

234
00:09:45,460 --> 00:09:47,350
then everything starts from the top.

235
00:09:47,350 --> 00:09:49,195
Now there is no saved state,

236
00:09:49,195 --> 00:09:52,180
so onSaveInstanceState will not be called

237
00:09:52,180 --> 00:09:55,033
and the Bundle passed to onCreate will be null.

238
00:09:55,033 --> 00:09:58,233
However, if the activity is destroyed as a result

239
00:09:58,233 --> 00:10:00,444
of a configuration change

240
00:10:00,444 --> 00:10:03,054
or because Android needs its resources,

241
00:10:03,054 --> 00:10:06,593
then Android, quote unquote, remembers, that it did exist

242
00:10:06,593 --> 00:10:08,393
and was killed by the system.

243
00:10:08,393 --> 00:10:10,622
And when the app is restarted by the user,

244
00:10:10,622 --> 00:10:12,168
its onCreate is called,

245
00:10:12,168 --> 00:10:15,237
with a Bundle containing the saved state.

246
00:10:15,237 --> 00:10:17,269
Now in the case of a configuration change,

247
00:10:17,269 --> 00:10:18,685
the same thing happens

248
00:10:18,685 --> 00:10:21,858
but the activity is restarted automatically.

249
00:10:21,858 --> 00:10:24,058
So there's no need for the user to launch it again.

250
00:10:24,058 --> 00:10:26,578
Now if Android kills an app that's in the foreground,

251
00:10:26,578 --> 00:10:28,054
then something's gone wrong.

252
00:10:28,054 --> 00:10:30,802
You'll always prefer to kill background processes

253
00:10:30,802 --> 00:10:33,374
to recover memory - not the foreground activity.

254
00:10:33,374 --> 00:10:35,157
And that's represented by the arrows on the

255
00:10:35,157 --> 00:10:37,562
left hand side of the slide.

256
00:10:37,562 --> 00:10:39,311
Now obviously, the user powering off the device

257
00:10:39,311 --> 00:10:42,370
is one case where a foreground activity would be destroyed.

258
00:10:42,370 --> 00:10:45,340
Now note that onDestroy may not always be called.

259
00:10:45,340 --> 00:10:46,479
So we'll see in a minute that

260
00:10:46,479 --> 00:10:49,511
onSaveinstanceState and onRestoreInstanceState

261
00:10:49,511 --> 00:10:51,458
aren't always called either.

262
00:10:51,458 --> 00:10:53,391
They're only called if the system detects

263
00:10:53,391 --> 00:10:55,671
there's is a need for them to be called.

264
00:10:55,671 --> 00:10:58,466
And as a result, the Google documentation recommends

265
00:10:58,466 --> 00:11:00,938
that you save user data in onStop,

266
00:11:00,938 --> 00:11:03,488
rather than onSaveInstanceState

267
00:11:03,488 --> 00:11:05,608
Alright, so that's the theory of how it all works,

268
00:11:05,608 --> 00:11:09,568
but how can we test it, and see what's actually happening.

269
00:11:09,568 --> 00:11:11,576
Now one very valuable technique

270
00:11:11,576 --> 00:11:13,966
is to implement all those methods, and put some code

271
00:11:13,966 --> 00:11:16,944
in them that logs the fact tha they're called,

272
00:11:16,944 --> 00:11:18,544
because that lets us see which methods are called,

273
00:11:18,544 --> 00:11:20,258
and what order as well.

274
00:11:20,258 --> 00:11:21,636
Now, you might think it's gonna

275
00:11:21,636 --> 00:11:22,827
be a bit tedious typing in

276
00:11:22,827 --> 00:11:24,693
all those methods that we've discussed

277
00:11:24,693 --> 00:11:25,937
when adding logging to them,

278
00:11:25,937 --> 00:11:27,556
but Android Studio helps with this,

279
00:11:27,556 --> 00:11:30,026
and will do most of the typing for us.

280
00:11:30,026 --> 00:11:32,197
So, I'm gonna switch back to Android Studio now

281
00:11:32,197 --> 00:11:35,646
and we'll have a look at how this all works.

282
00:11:35,646 --> 00:11:37,535
Alright, lets start with the logging.

283
00:11:37,535 --> 00:11:40,906
Now Android Studio used to have, what was called,

284
00:11:40,906 --> 00:11:43,626
Live Templates, which are defined for us

285
00:11:43,626 --> 00:11:45,985
to type in many of the routine bits of code

286
00:11:45,985 --> 00:11:48,127
that are used over and over again.

287
00:11:48,127 --> 00:11:50,845
And one set deals with writing to the logfile.

288
00:11:50,845 --> 00:11:53,066
However, these are being phased out,

289
00:11:53,066 --> 00:11:56,974
so if you're used to using the logt shortcut, for example,

290
00:11:56,974 --> 00:11:58,936
well that won't work in kotlin.

291
00:11:58,936 --> 00:12:00,928
The indication is that it won't ever work,

292
00:12:00,928 --> 00:12:03,932
and Android Studio will have some other mechanism instead.

293
00:12:03,932 --> 00:12:05,832
Now as I don't know what that is at the moment,

294
00:12:05,832 --> 00:12:08,113
we're gonna have to do things the longer way.

295
00:12:08,113 --> 00:12:09,904
Now, I don't wanna go into a lot of detail

296
00:12:09,904 --> 00:12:11,494
about logging at this stage,

297
00:12:11,494 --> 00:12:14,513
and we're going to write debug log entries to the logcat,

298
00:12:14,513 --> 00:12:16,673
so whenever we want to do that.

299
00:12:16,673 --> 00:12:19,189
Now we're going to write debug log entries to the logcat,

300
00:12:19,189 --> 00:12:20,814
so whenever you wanna do that, just use

301
00:12:20,814 --> 00:12:22,743
the code I'm about to type,

302
00:12:22,743 --> 00:12:25,237
and we're going to go into more detail on logging later.

303
00:12:25,237 --> 00:12:27,888
Now the Android Log takes two arguments.

304
00:12:27,888 --> 00:12:30,806
One is a Tag, which usually just identifies the class

305
00:12:30,806 --> 00:12:32,566
that the log entries are coming from.

306
00:12:32,566 --> 00:12:34,288
And because we're gonna be using the same tag

307
00:12:34,288 --> 00:12:36,019
for all entries in this class,

308
00:12:36,019 --> 00:12:38,088
I'm gonna create a Constant for it,

309
00:12:38,088 --> 00:12:39,328
right at the top of the class.

310
00:12:39,328 --> 00:12:40,907
So lets go ahead and do that,

311
00:12:40,907 --> 00:12:43,867
and I'll actually add it

312
00:12:43,867 --> 00:12:45,460
down here, before the class definitions.

313
00:12:45,460 --> 00:12:49,890
I'm gonna do private, (typing) val, TAG.

314
00:12:49,890 --> 00:12:52,248
I'm putting tag in uppercase deliberately.

315
00:12:52,248 --> 00:12:55,748
It'll set that equal to MainActivity.

316
00:12:57,589 --> 00:13:00,006
Now that we've done that, we can use that TAG

317
00:13:00,006 --> 00:13:01,896
whenever we want to log something.

318
00:13:01,896 --> 00:13:03,621
So what I'm going to do is start a new line

319
00:13:03,621 --> 00:13:05,928
in the onCreate method now.

320
00:13:05,928 --> 00:13:08,189
I'm gonna put that as the first line

321
00:13:08,189 --> 00:13:10,219
and add a debug log call.

322
00:13:10,219 --> 00:13:14,720
So I'm going to type, Log with a capital L, dot d.

323
00:13:14,720 --> 00:13:18,998
Then in parentheses it's going to be TAG in uppercase,

324
00:13:18,998 --> 00:13:22,748
comma, and then I'm going to type onCreate,

325
00:13:23,845 --> 00:13:27,757
(typing) colon called.

326
00:13:27,757 --> 00:13:30,259
So we've added a statement here that it's, ultimately,

327
00:13:30,259 --> 00:13:32,688
going to add a debug log call.

328
00:13:32,688 --> 00:13:34,304
And lets go ahead and do the same

329
00:13:34,304 --> 00:13:35,984
in the onClickListener as well,

330
00:13:35,984 --> 00:13:38,085
just in the first line down here,

331
00:13:38,085 --> 00:13:40,085
in the onClick function.

332
00:13:41,645 --> 00:13:43,645
So I'm gonna type Log.d

333
00:13:44,645 --> 00:13:46,494
and TAG in parentheses again,

334
00:13:46,494 --> 00:13:48,736
and we're going to put, in double quotes then,

335
00:13:48,736 --> 00:13:52,236
onClick, (typing) called.

336
00:13:54,296 --> 00:13:55,715
And that's actually it.

337
00:13:55,715 --> 00:13:57,375
Now before we start doing anything else

338
00:13:57,375 --> 00:13:59,395
and looking at those lifecycle functions,

339
00:13:59,395 --> 00:14:02,376
let's actually run the app to see what we've done here.

340
00:14:02,376 --> 00:14:04,959
So I'm gonna go ahead and do that.

341
00:14:08,355 --> 00:14:11,696
Check the app is actually running.

342
00:14:11,696 --> 00:14:13,896
There's our app running,

343
00:14:13,896 --> 00:14:17,146
and if I have a look now at the logcat,

344
00:14:18,444 --> 00:14:21,522
we can see here we've got onCreate called.

345
00:14:21,522 --> 00:14:24,122
That's the output there that we've typed,

346
00:14:24,122 --> 00:14:25,979
and at the moment the other one was

347
00:14:25,979 --> 00:14:27,333
when the button was clicked.

348
00:14:27,333 --> 00:14:29,464
So obviously, we don't see the text for that and

349
00:14:29,464 --> 00:14:31,369
if we swing over there now,

350
00:14:31,369 --> 00:14:33,575
and type in something, "Tim"

351
00:14:33,575 --> 00:14:35,867
and then click on the button,

352
00:14:35,867 --> 00:14:37,976
you can see down here, in the log,

353
00:14:37,976 --> 00:14:42,214
log view, that onClick was actually called.

354
00:14:42,214 --> 00:14:45,136
Now before we start looking at those lifecycle functions,

355
00:14:45,136 --> 00:14:48,488
we should really run this app to see what we've done here.

356
00:14:48,488 --> 00:14:51,197
But what I'm going to do is, firstly, come back over here

357
00:14:51,197 --> 00:14:53,717
because we're still in project view,

358
00:14:53,717 --> 00:14:55,077
and go back to Android view just

359
00:14:55,077 --> 00:14:56,825
so things are a little bit easier for us to see.

360
00:14:56,825 --> 00:14:58,437
And I'm just gonna expand this out

361
00:14:58,437 --> 00:15:01,008
so we can see our apps.

362
00:15:01,008 --> 00:15:03,008
Alright so I'm gonna stop the video here,

363
00:15:03,008 --> 00:15:04,437
and we're going to look at those lifecycle

364
00:15:04,437 --> 00:15:06,277
functions in the next video.

365
00:15:06,277 --> 00:15:08,650
And we're going to be looking at logcat in detail,

366
00:15:08,650 --> 00:15:10,618
and it's probably something that you wanna review

367
00:15:10,618 --> 00:15:12,882
a couple of times, so a separate video

368
00:15:12,882 --> 00:15:14,071
will make that easier.

369
00:15:14,071 --> 00:15:16,988
So lets do that in the next video.

