1
00:00:04,660 --> 00:00:07,000
Alright, so it's time to start creating

2
00:00:07,000 --> 00:00:09,460
our app to download that RSS feed, and

3
00:00:09,460 --> 00:00:12,730
display the data onto the screen. Now the

4
00:00:12,730 --> 00:00:14,080
app itself is going to be pretty basic

5
00:00:14,080 --> 00:00:16,270
but it will demonstrate some very

6
00:00:16,270 --> 00:00:19,090
important concepts, including performing

7
00:00:19,090 --> 00:00:21,579
operations on a separate thread, so that

8
00:00:21,579 --> 00:00:23,650
the app doesn't appear to freeze while

9
00:00:23,650 --> 00:00:26,230
it's performing a long-running task. Now

10
00:00:26,230 --> 00:00:27,849
although it's getting rarer these days,

11
00:00:27,849 --> 00:00:30,040
you may have actually seen android pop

12
00:00:30,040 --> 00:00:30,399
up

13
00:00:30,399 --> 00:00:33,280
ANR dialogues - ANR, by the way, stands

14
00:00:33,280 --> 00:00:35,500
for application not responding, although

15
00:00:35,500 --> 00:00:37,360
Google have changed the way that the

16
00:00:37,360 --> 00:00:40,120
dialogues appear. In fact, I can make one

17
00:00:40,120 --> 00:00:42,520
appear quite easily, by doing something

18
00:00:42,520 --> 00:00:44,470
incredibly foolish with a few lines of

19
00:00:44,470 --> 00:00:46,930
code. I'm actually going to add a loop to

20
00:00:46,930 --> 00:00:48,880
the onCreate method. What I'll do

21
00:00:48,880 --> 00:00:50,140
first, though, is I'm going to start my

22
00:00:50,140 --> 00:00:52,570
emulator, so that it's ready. And we're

23
00:00:52,570 --> 00:00:55,540
going to type in some code, while that's

24
00:00:55,540 --> 00:00:58,600
actually starting. So again, we're

25
00:00:58,600 --> 00:01:00,490
going to add this loop - we're going to

26
00:01:00,490 --> 00:01:02,410
add this after the setContentView line,

27
00:01:02,410 --> 00:01:06,119
and the loop's just going to say while

28
00:01:06,119 --> 00:01:09,789
parentheses true, open the code block and

29
00:01:09,789 --> 00:01:12,369
then we're going to put val x, lower case

30
00:01:12,369 --> 00:01:15,299
val x equals 1, and that's actually it.

31
00:01:15,299 --> 00:01:18,069
Now, in case it's not obvious, don't ever

32
00:01:18,069 --> 00:01:21,009
do anything like this. What I've done is

33
00:01:21,009 --> 00:01:22,960
created a loop that just goes round and

34
00:01:22,960 --> 00:01:25,060
round forever, but doesn't do anything

35
00:01:25,060 --> 00:01:27,999
other than give x the value of 1. So it's

36
00:01:27,999 --> 00:01:30,159
a really pointless thing to do, but it

37
00:01:30,159 --> 00:01:32,170
will prevent the onCreate method from

38
00:01:32,170 --> 00:01:34,299
finishing, and will make the app freeze.

39
00:01:34,299 --> 00:01:36,429
So when I go ahead now and run this on

40
00:01:36,429 --> 00:01:42,630
my emulator -

41
00:01:42,630 --> 00:01:47,590
give that a moment to compile.

42
00:01:47,590 --> 00:01:49,689
Now we don't even get to see the usual

43
00:01:49,689 --> 00:01:51,969
Hello World that Android Studio includes

44
00:01:51,969 --> 00:01:54,039
in the default layout, and that's because

45
00:01:54,039 --> 00:01:57,520
the display doesn't appear until after

46
00:01:57,520 --> 00:02:00,189
onCreate terminates. So if I come over to

47
00:02:00,189 --> 00:02:02,289
the app now. If I click on the back

48
00:02:02,289 --> 00:02:05,679
button to stop the app, there's a short

49
00:02:05,679 --> 00:02:07,240
pause, and what should happen is the

50
00:02:07,240 --> 00:02:08,860
Android framework will now detect that

51
00:02:08,860 --> 00:02:11,110
the app's frozen, and you can see that

52
00:02:11,110 --> 00:02:12,430
that it's popped up on the screen now and,

53
00:02:12,430 --> 00:02:14,739
at this point here, I can either wait or

54
00:02:14,739 --> 00:02:17,650
close the app. And sometimes the app

55
00:02:17,650 --> 00:02:19,239
really is doing something and you might

56
00:02:19,239 --> 00:02:21,040
decide to wait, but I'm going to close

57
00:02:21,040 --> 00:02:23,500
this one because it's never going to be

58
00:02:23,500 --> 00:02:24,790
doing anything useful. So I'm gonna click

59
00:02:24,790 --> 00:02:27,310
on Close app. So that was an extreme

60
00:02:27,310 --> 00:02:29,680
example, but you can see that Android

61
00:02:29,680 --> 00:02:31,930
doesn't like it when an app takes over

62
00:02:31,930 --> 00:02:34,480
the device, and doesn't finish its job in

63
00:02:34,480 --> 00:02:36,730
a reasonable amount of time. And there's

64
00:02:36,730 --> 00:02:37,900
a very good reason for that,

65
00:02:37,900 --> 00:02:39,640
which is that the user interface is

66
00:02:39,640 --> 00:02:42,220
shared by all apps. So there's only one

67
00:02:42,220 --> 00:02:44,769
screen on the device, so if one app is

68
00:02:44,769 --> 00:02:46,629
taking it over and it refuses to

69
00:02:46,629 --> 00:02:48,790
release it, then the device, effectively,

70
00:02:48,790 --> 00:02:50,700
can't be used for anything else.

71
00:02:50,700 --> 00:02:52,690
Now if you remember from the

72
00:02:52,690 --> 00:02:54,510
discussion of the activity lifecycle,

73
00:02:54,510 --> 00:02:57,310
when another activity is started, it

74
00:02:57,310 --> 00:02:59,500
doesn't become visible until the on

75
00:02:59,500 --> 00:03:01,600
Pause method of the current activity

76
00:03:01,600 --> 00:03:04,060
finishes. Now this activity with the

77
00:03:04,060 --> 00:03:05,530
infinite loop will never finish,

78
00:03:05,530 --> 00:03:07,450
so if Android doesn't provide that

79
00:03:07,450 --> 00:03:09,700
dialog as a way to kill the app, then the

80
00:03:09,700 --> 00:03:11,410
device, effectively, couldn't do anything

81
00:03:11,410 --> 00:03:13,840
else. Now remember that activities are

82
00:03:13,840 --> 00:03:16,599
being interrupted all the time. One thing

83
00:03:16,599 --> 00:03:17,919
that often interrupts what you're doing

84
00:03:17,919 --> 00:03:20,380
on a phone, is an incoming phone call. If

85
00:03:20,380 --> 00:03:22,299
your app prevents users from answering

86
00:03:22,299 --> 00:03:23,859
the phone, well frankly, they're not going

87
00:03:23,859 --> 00:03:26,230
to be very impressed. In fact, that won't

88
00:03:26,230 --> 00:03:28,180
actually happened. Android is now

89
00:03:28,180 --> 00:03:30,280
cleverer than that, and will let things

90
00:03:30,280 --> 00:03:32,380
like an incoming phone call take over,

91
00:03:32,380 --> 00:03:35,049
but the point still holds. If the user

92
00:03:35,049 --> 00:03:37,389
wanted to make a phone call, they'd have to

93
00:03:37,389 --> 00:03:39,549
close our rogue app first, and we've just

94
00:03:39,549 --> 00:03:41,889
seen that it doesn't close until we tell

95
00:03:41,889 --> 00:03:44,400
Android to kill it. So all of this is

96
00:03:44,400 --> 00:03:46,510
really, really should hopefully

97
00:03:46,510 --> 00:03:48,489
emphasize the point that it's important

98
00:03:48,489 --> 00:03:50,560
that we think about what our apps are

99
00:03:50,560 --> 00:03:53,019
actually doing, and we don't perform long

100
00:03:53,019 --> 00:03:55,840
running tasks on the main thread. Doing

101
00:03:55,840 --> 00:03:58,750
so will block the user interface. Now

102
00:03:58,750 --> 00:04:00,610
you'll find the documentation referring

103
00:04:00,610 --> 00:04:01,480
to the UI thread,

104
00:04:01,480 --> 00:04:03,459
which means the thread that the user

105
00:04:03,459 --> 00:04:06,220
interface is running on. So when we do

106
00:04:06,220 --> 00:04:08,080
something like downloading data over the

107
00:04:08,080 --> 00:04:09,819
Internet, we don't really have any

108
00:04:09,819 --> 00:04:12,550
control over how long the process will

109
00:04:12,550 --> 00:04:15,340
take. Sometimes the Internet can be very

110
00:04:15,340 --> 00:04:17,350
slow, and sometimes even sites such as

111
00:04:17,350 --> 00:04:19,418
Apple's can go down and become

112
00:04:19,418 --> 00:04:21,339
unavailable. Now that would obviously

113
00:04:21,339 --> 00:04:23,470
cause our download to take a long time,

114
00:04:23,470 --> 00:04:25,840
and could cause our app to freeze while

115
00:04:25,840 --> 00:04:27,280
it's waiting for that download to

116
00:04:27,280 --> 00:04:30,220
complete. So the way to cope with this is

117
00:04:30,220 --> 00:04:32,130
to run the download on a separate thread.

118
00:04:32,130 --> 00:04:34,510
Now think of a thread as a way of

119
00:04:34,510 --> 00:04:36,340
running code at the same time as other

120
00:04:36,340 --> 00:04:39,430
code is also running. In fact, with modern

121
00:04:39,430 --> 00:04:41,770
multiprocessor or multi-core devices,

122
00:04:41,770 --> 00:04:44,320
that's exactly what does happen. The

123
00:04:44,320 --> 00:04:46,270
thread will run on another processor or

124
00:04:46,270 --> 00:04:48,790
core, while the user interface continues to

125
00:04:48,790 --> 00:04:51,340
execute. Now writing multi-threaded

126
00:04:51,340 --> 00:04:53,410
applications is notoriously difficult,

127
00:04:53,410 --> 00:04:56,020
and even experienced programmers

128
00:04:56,020 --> 00:04:58,720
struggle to get it right. But fortunately,

129
00:04:58,720 --> 00:05:00,940
Android provides a class that takes care

130
00:05:00,940 --> 00:05:03,760
of all of the complexity for us. So we

131
00:05:03,760 --> 00:05:06,250
can use an AsyncTask to perform the

132
00:05:06,250 --> 00:05:08,740
download on a separate thread, and then

133
00:05:08,740 --> 00:05:10,780
get notified when the download is

134
00:05:10,780 --> 00:05:13,000
finished. So the download will happen in

135
00:05:13,000 --> 00:05:14,770
the background, and the user interface

136
00:05:14,770 --> 00:05:17,050
throughout the UI thread, will receive

137
00:05:17,050 --> 00:05:19,570
the results of the download. So the

138
00:05:19,570 --> 00:05:21,180
download will happen asynchronously,

139
00:05:21,180 --> 00:05:23,710
which just means in the background on a

140
00:05:23,710 --> 00:05:25,870
separate thread, and our app can get on

141
00:05:25,870 --> 00:05:27,490
with other things while that's going on.

142
00:05:27,490 --> 00:05:29,590
Now we can even switch to another app

143
00:05:29,590 --> 00:05:31,300
and do something else while an async

144
00:05:31,300 --> 00:05:33,430
task is running. This app won't allow

145
00:05:33,430 --> 00:05:35,139
that, but we'll see ways to make that

146
00:05:35,139 --> 00:05:37,210
possible later in the course. So

147
00:05:37,210 --> 00:05:38,710
basically, we tell the Android framework

148
00:05:38,710 --> 00:05:41,350
to get on with the task, then forget

149
00:05:41,350 --> 00:05:44,470
about it until that task finishes. So

150
00:05:44,470 --> 00:05:47,080
that's the theory, at least. Unfortunately,

151
00:05:47,080 --> 00:05:48,880
though, using an AsyncTask to

152
00:05:48,880 --> 00:05:50,740
communicate with the UI thread in Kotlin,

153
00:05:50,740 --> 00:05:53,979
isn't quite as easy as it sounds. Now I'm

154
00:05:53,979 --> 00:05:56,560
going to go ahead and use one anyway, but

155
00:05:56,560 --> 00:05:58,180
in subsequent videos, we're going to

156
00:05:58,180 --> 00:06:00,310
delete all this code and do it a bit

157
00:06:00,310 --> 00:06:02,440
differently. Now there are a couple of

158
00:06:02,440 --> 00:06:04,960
reasons for doing this. One is that most

159
00:06:04,960 --> 00:06:06,940
of the google examples are still written

160
00:06:06,940 --> 00:06:09,280
in Java, and probably will be for a while

161
00:06:09,280 --> 00:06:11,289
yet. So that means that you're going to

162
00:06:11,289 --> 00:06:13,419
be seeing the the Java way of doing

163
00:06:13,419 --> 00:06:15,580
things, and the Kotlin way's often

164
00:06:15,580 --> 00:06:18,370
very different. So this example will show

165
00:06:18,370 --> 00:06:21,039
just how different it can be. Now the

166
00:06:21,039 --> 00:06:22,840
other reason is that Android Studio will

167
00:06:22,840 --> 00:06:25,780
actually convert Java code to Kotlin for

168
00:06:25,780 --> 00:06:27,879
us. In fact, that's how I produced his

169
00:06:27,879 --> 00:06:29,530
first lot of code that we're going to

170
00:06:29,530 --> 00:06:31,930
see. I had to do a bit of tweaking but,

171
00:06:31,930 --> 00:06:34,539
ultimately, that did work. However, what

172
00:06:34,539 --> 00:06:36,610
it produces is basically Java code,

173
00:06:36,610 --> 00:06:39,460
written with a Kotlin syntax. Now you

174
00:06:39,460 --> 00:06:41,050
will find Kotlin code that's been

175
00:06:41,050 --> 00:06:42,849
produced in this way, and you may even

176
00:06:42,849 --> 00:06:45,219
end up having to maintain some. But

177
00:06:45,219 --> 00:06:47,020
hopefully, by the time you've worked

178
00:06:47,020 --> 00:06:49,120
through these next few videos, you'll be

179
00:06:49,120 --> 00:06:50,919
better positioned to spot code like that,

180
00:06:50,919 --> 00:06:53,800
and more importantly, you won't write any

181
00:06:53,800 --> 00:06:56,250
more yourself. Alright, so let's start.

182
00:06:56,250 --> 00:06:58,300
First thing I'm going to do is delete

183
00:06:58,300 --> 00:07:00,069
that rogue while loop that we don't

184
00:07:00,069 --> 00:07:01,479
want. So I'm going to come over here and

185
00:07:01,479 --> 00:07:04,990
delete those three lines. Now most of the

186
00:07:04,990 --> 00:07:06,819
time, new classes go into their own

187
00:07:06,819 --> 00:07:09,370
Kotlin file, but this class is going to

188
00:07:09,370 --> 00:07:11,319
access properties of our MainActivity,

189
00:07:11,319 --> 00:07:13,240
so therefore, I'm going to create it as

190
00:07:13,240 --> 00:07:15,550
an inner class. So normally, I'd

191
00:07:15,550 --> 00:07:17,080
right-click on the package and choose

192
00:07:17,080 --> 00:07:19,509
New then Kotlin Class, and that would

193
00:07:19,509 --> 00:07:22,000
create a new class. But here we don't

194
00:07:22,000 --> 00:07:23,560
want to do that, because we're going to

195
00:07:23,560 --> 00:07:25,539
create the class inside the Main

196
00:07:25,539 --> 00:07:28,300
Activity class. So I'm going to call this

197
00:07:28,300 --> 00:07:28,750
one

198
00:07:28,750 --> 00:07:31,089
download data, and it's just the same as

199
00:07:31,089 --> 00:07:33,250
any other Kotlin class, except that it

200
00:07:33,250 --> 00:07:36,039
can only be used by MainActivity. So in

201
00:07:36,039 --> 00:07:37,360
other words, it's not visible to any

202
00:07:37,360 --> 00:07:39,370
other class in the program. So I come

203
00:07:39,370 --> 00:07:41,819
down here and add this code. I'm going to

204
00:07:41,819 --> 00:07:44,219
clean that space up and then below the

205
00:07:44,219 --> 00:07:46,870
onCreate function, I'm going to type

206
00:07:46,870 --> 00:07:55,210
private inner class DownloadData : I'm

207
00:07:55,210 --> 00:07:58,509
gonna type AsyncTask. Press Enter there,

208
00:07:58,509 --> 00:08:02,259
and I'm going to add the diamonds and then

209
00:08:02,259 --> 00:08:03,940
put string - or add a diamond operator,

210
00:08:03,940 --> 00:08:08,610
I should say - String comma Void comma

211
00:08:08,610 --> 00:08:12,909
String. Then at the right of the diamond

212
00:08:12,909 --> 00:08:14,430
operator I'm going to add parentheses.

213
00:08:14,430 --> 00:08:19,409
Then add the left and right curly braces.

214
00:08:19,409 --> 00:08:21,339
Now at the moment, we've got an error

215
00:08:21,339 --> 00:08:23,830
there, but what I've done is, I've created

216
00:08:23,830 --> 00:08:27,490
a new class that extends the AsyncTask

217
00:08:27,490 --> 00:08:28,810
class.

218
00:08:28,810 --> 00:08:31,630
Now extending an existing class, which is

219
00:08:31,630 --> 00:08:34,059
also known as sub-classing, lets you take

220
00:08:34,059 --> 00:08:35,950
advantage of all the processing in the

221
00:08:35,950 --> 00:08:38,200
existing class. So here what we're doing,

222
00:08:38,200 --> 00:08:40,960
is taking advantage of the asynchronous

223
00:08:40,960 --> 00:08:43,120
processing that the Android framework

224
00:08:43,120 --> 00:08:45,340
provides. And all we have to do is

225
00:08:45,340 --> 00:08:47,470
provide the code for the task that we

226
00:08:47,470 --> 00:08:49,510
want to perform, and then let Android

227
00:08:49,510 --> 00:08:51,520
take care of all the multi-threading

228
00:08:51,520 --> 00:08:54,280
complexity for us. Now we need to set up

229
00:08:54,280 --> 00:08:56,620
three parameters when we create an Async

230
00:08:56,620 --> 00:08:58,780
Task, so that Android knows what type

231
00:08:58,780 --> 00:09:01,330
of task we're dealing with. Now here we're

232
00:09:01,330 --> 00:09:02,650
saying that the information that we'll

233
00:09:02,650 --> 00:09:04,780
provide to the task, will be of type

234
00:09:04,780 --> 00:09:07,480
string. So we're going to pass in the URL,

235
00:09:07,480 --> 00:09:09,910
in the RSS feed, and we're going to store

236
00:09:09,910 --> 00:09:12,370
that as a string. This is that first

237
00:09:12,370 --> 00:09:15,550
parameter here, first argument - string. Now

238
00:09:15,550 --> 00:09:17,350
the second parameter's used if we want

239
00:09:17,350 --> 00:09:19,000
to display a progress bar - that's this

240
00:09:19,000 --> 00:09:19,450
one.

241
00:09:19,450 --> 00:09:22,660
Now our download is quite small, so there

242
00:09:22,660 --> 00:09:24,520
won't be time for a progress bar to be

243
00:09:24,520 --> 00:09:27,010
displayed. Now if we were downloading

244
00:09:27,010 --> 00:09:29,080
large amounts of data, that could perhaps

245
00:09:29,080 --> 00:09:30,790
take a few minutes or more to download,

246
00:09:30,790 --> 00:09:32,830
then it might be a good idea to give the

247
00:09:32,830 --> 00:09:34,690
user some idea of how long it'll take,

248
00:09:34,690 --> 00:09:37,540
and also how far through we were. But

249
00:09:37,540 --> 00:09:38,800
as we're not going to do that, I'm

250
00:09:38,800 --> 00:09:41,320
making the second type here void, to show

251
00:09:41,320 --> 00:09:43,600
that we're not actually using that. Now

252
00:09:43,600 --> 00:09:46,000
onto the third parameter - this is the

253
00:09:46,000 --> 00:09:47,830
type of the result that we want to get

254
00:09:47,830 --> 00:09:51,220
back. So all our XML will be a string, so

255
00:09:51,220 --> 00:09:52,630
we're using string again for the third

256
00:09:52,630 --> 00:09:55,120
parameter type. Now you can find out more

257
00:09:55,120 --> 00:09:57,640
information on the AsyncTask class in

258
00:09:57,640 --> 00:10:00,310
Google's documentation. Let's just

259
00:10:00,310 --> 00:10:02,860
open it up briefly. I'm just going to

260
00:10:02,860 --> 00:10:09,150
paste this over the top of our RSS feed.

261
00:10:09,150 --> 00:10:12,010
Now at the moment, as I mentioned, a lot

262
00:10:12,010 --> 00:10:13,990
of Google's examples, and including this

263
00:10:13,990 --> 00:10:15,880
one, as at the time of recording this

264
00:10:15,880 --> 00:10:18,640
video, is still in Java and not Kotlin,

265
00:10:18,640 --> 00:10:20,650
unfortunately, and that's one reason why

266
00:10:20,650 --> 00:10:22,180
Kotlin programmers need to have some

267
00:10:22,180 --> 00:10:25,420
familiarity with the Java language. Now

268
00:10:25,420 --> 00:10:27,310
we will look at how Android Studio can

269
00:10:27,310 --> 00:10:29,650
make life a lot easier for us, so don't

270
00:10:29,650 --> 00:10:31,060
think, by any means, that you need to be

271
00:10:31,060 --> 00:10:33,700
an expert Java programmer to work

272
00:10:33,700 --> 00:10:36,190
through this course. Alright, so that's

273
00:10:36,190 --> 00:10:38,050
the three parameters that I've talked

274
00:10:38,050 --> 00:10:41,290
about here, that we need to define when

275
00:10:41,290 --> 00:10:42,810
we setup an AsyncTask,

276
00:10:42,810 --> 00:10:45,520
so that Android knows what type of task

277
00:10:45,520 --> 00:10:47,770
we're dealing with. The first parameter,

278
00:10:47,770 --> 00:10:49,360
again, will be a string containing, or

279
00:10:49,360 --> 00:10:51,580
holding, the address of the RSS feed.

280
00:10:51,580 --> 00:10:54,310
We're not using the second parameter at

281
00:10:54,310 --> 00:10:55,960
all, and the third will be the string

282
00:10:55,960 --> 00:10:57,970
containing all the XML that we've

283
00:10:57,970 --> 00:11:00,880
downloaded. Now Android Studio's

284
00:11:00,880 --> 00:11:02,410
actually showing an error here,

285
00:11:02,410 --> 00:11:03,970
and that's because if we're going to be

286
00:11:03,970 --> 00:11:06,190
using an AsyncTask, then there are some

287
00:11:06,190 --> 00:11:08,830
functions that we need to implement. Well,

288
00:11:08,830 --> 00:11:10,180
actually, there's only one function that

289
00:11:10,180 --> 00:11:12,220
we must implement, and that's the doin

290
00:11:12,220 --> 00:11:13,660
Background function. We can hover

291
00:11:13,660 --> 00:11:15,910
over there - you can see that it's telling

292
00:11:15,910 --> 00:11:18,220
us that it's really looking for this

293
00:11:18,220 --> 00:11:20,320
doInBackground. It's basically saying

294
00:11:20,320 --> 00:11:22,630
that the class DownloaData is not

295
00:11:22,630 --> 00:11:24,910
abstract and does not implement an

296
00:11:24,910 --> 00:11:27,940
abstract base class member. But as it

297
00:11:27,940 --> 00:11:29,320
turns out, we're also going to implement

298
00:11:29,320 --> 00:11:31,780
another function, though. Because we want

299
00:11:31,780 --> 00:11:33,460
to get data back when the task completes,

300
00:11:33,460 --> 00:11:36,040
we need to implement the on post

301
00:11:36,040 --> 00:11:38,890
execute function. Android will call that

302
00:11:38,890 --> 00:11:41,140
function when the job's done. Without

303
00:11:41,140 --> 00:11:42,700
that, there wouldn't be a way for us to

304
00:11:42,700 --> 00:11:44,410
know that the task had finished, and

305
00:11:44,410 --> 00:11:47,050
therefore we wouldn't get the result. Now

306
00:11:47,050 --> 00:11:49,300
we can get Android Studio to create the

307
00:11:49,300 --> 00:11:51,970
base stubs for the methods, by using ctrl

308
00:11:51,970 --> 00:11:54,130
o to get a list of the methods that are

309
00:11:54,130 --> 00:11:56,830
available to override or implement. You

310
00:11:56,830 --> 00:11:58,510
do want to make sure that your cursor is

311
00:11:58,510 --> 00:12:00,550
in the inner class for that to happen,

312
00:12:00,550 --> 00:12:02,680
and once you do that, you can just do a

313
00:12:02,680 --> 00:12:05,440
ctrl o, and that brings up a list of

314
00:12:05,440 --> 00:12:07,600
potential functions that can be

315
00:12:07,600 --> 00:12:09,640
overridden. And, of course, we saw that in

316
00:12:09,640 --> 00:12:11,110
an earlier video when we overrode the

317
00:12:11,110 --> 00:12:14,080
methods in the Activity Lifecycle. so as

318
00:12:14,080 --> 00:12:15,280
you can see, there are a few methods

319
00:12:15,280 --> 00:12:17,320
available, and if we're updating a

320
00:12:17,320 --> 00:12:19,270
progress bar then we want to do that in

321
00:12:19,270 --> 00:12:21,190
the on, we want to use rather, the on

322
00:12:21,190 --> 00:12:24,100
ProgressUpdate function, and the on

323
00:12:24,100 --> 00:12:26,860
PreExecute function would be used to set up

324
00:12:26,860 --> 00:12:29,320
the progress bar, for example, but can

325
00:12:29,320 --> 00:12:30,580
also be used with setting up anything

326
00:12:30,580 --> 00:12:32,200
else that's needed for our task to run.

327
00:12:32,200 --> 00:12:34,810
Now both of those methods are called on

328
00:12:34,810 --> 00:12:37,240
the main UI thread, otherwise they

329
00:12:37,240 --> 00:12:39,310
wouldn't be able to display anything. So

330
00:12:39,310 --> 00:12:41,380
it's important not to do anything like

331
00:12:41,380 --> 00:12:44,590
trying to start the download in onPre

332
00:12:44,590 --> 00:12:46,990
Execute. Now the other method we want,

333
00:12:46,990 --> 00:12:50,260
though, is onPostExecute, which also runs

334
00:12:50,260 --> 00:12:52,090
on the main UI thread, once the

335
00:12:52,090 --> 00:12:54,370
background process has completed. So,

336
00:12:54,370 --> 00:12:55,930
therefore, we're going to choose both

337
00:12:55,930 --> 00:12:56,420
doInBackground

338
00:12:56,420 --> 00:12:59,089
and we're gonna choose

339
00:12:59,089 --> 00:13:01,250
onPostExecute, which was already selected,

340
00:13:01,250 --> 00:13:05,360
and then we're going to click on Ok. And

341
00:13:05,360 --> 00:13:07,010
you can see that those two functions

342
00:13:07,010 --> 00:13:08,990
have now been created, and the error

343
00:13:08,990 --> 00:13:11,540
message has disappeared. Alright, so I'm

344
00:13:11,540 --> 00:13:12,949
going to start the code to do the

345
00:13:12,949 --> 00:13:15,470
downloading in the next video. But what

346
00:13:15,470 --> 00:13:17,000
I'm going to do for now is just put some

347
00:13:17,000 --> 00:13:19,610
logging into these functions, so that we

348
00:13:19,610 --> 00:13:21,560
can see what's going on and get a feel

349
00:13:21,560 --> 00:13:24,440
for how the AsyncTask works. So we're

350
00:13:24,440 --> 00:13:26,690
going to need a log tag for each of our

351
00:13:26,690 --> 00:13:28,850
classes. So I'm going to come up, firstly,

352
00:13:28,850 --> 00:13:31,519
to our MainActivity, and the first line

353
00:13:31,519 --> 00:13:33,170
after the definition for our Main

354
00:13:33,170 --> 00:13:36,190
Activity, I'm going to type private val,

355
00:13:36,190 --> 00:13:39,889
TAG in uppercase, equals double quotes

356
00:13:39,889 --> 00:13:43,910
MainActivity, in double quotes. So we can

357
00:13:43,910 --> 00:13:46,670
now use that tag when logging in Main

358
00:13:46,670 --> 00:13:49,040
Activity's onCreate function, and we can go

359
00:13:49,040 --> 00:13:54,040
ahead and do that. We can type log.d

360
00:13:54,040 --> 00:13:57,589
parentheses and tag comma,

361
00:13:57,589 --> 00:14:05,959
and we can just type onCreate called. Let's

362
00:14:05,959 --> 00:14:07,940
do the same for the DownloadData inner

363
00:14:07,940 --> 00:14:09,430
class. So I'm going to come down into the

364
00:14:09,430 --> 00:14:12,110
first, or the first line after the definition,

365
00:14:12,110 --> 00:14:16,040
and we're going to type private val TAG

366
00:14:16,040 --> 00:14:22,070
equals DownloadData. Alright, so now that

367
00:14:22,070 --> 00:14:23,990
I've added those tags, we'll finish the

368
00:14:23,990 --> 00:14:26,320
video here. In the next video, we'll start

369
00:14:26,320 --> 00:14:29,209
continuing on with this program, and

370
00:14:29,209 --> 00:14:30,440
we're going to start, in that next video,

371
00:14:30,440 --> 00:14:33,079
by looking at the warnings that Android

372
00:14:33,079 --> 00:14:35,180
Studio are popping up. So I'll see you in

373
00:14:35,180 --> 00:14:37,750
the next video.

