1
00:00:05,473 --> 00:00:08,170
So now that we've created our simple task class,

2
00:00:08,170 --> 00:00:11,630
we can create the activity to handle adding tasks.

3
00:00:11,630 --> 00:00:13,510
Now I said activity here,

4
00:00:13,510 --> 00:00:16,129
but instead of creating a new activity,

5
00:00:16,129 --> 00:00:18,370
we're going to create a new fragment.

6
00:00:18,370 --> 00:00:19,790
So what is a fragment?

7
00:00:19,790 --> 00:00:21,890
Well, a good description of a fragment

8
00:00:21,890 --> 00:00:24,180
is that it's a sub activity.

9
00:00:24,180 --> 00:00:26,460
A fragment can have it's own layout

10
00:00:26,460 --> 00:00:29,410
and can be embedded inside an activities layout

11
00:00:29,410 --> 00:00:32,383
to provide some functionality in a modular fashion.

12
00:00:33,240 --> 00:00:35,200
So, we quickly skip over to Google

13
00:00:35,200 --> 00:00:37,773
and do a search for Android Fragment.

14
00:00:40,947 --> 00:00:42,770
You get this link that usually comes up first

15
00:00:42,770 --> 00:00:46,953
from Android, under a guide for components and fragments.

16
00:00:46,953 --> 00:00:48,600
So I'm gonna click on that.

17
00:00:48,600 --> 00:00:50,860
And this is well worth a read.

18
00:00:50,860 --> 00:00:52,820
So the basic idea of a fragment

19
00:00:52,820 --> 00:00:56,280
is that you can reuse it in different activities

20
00:00:56,280 --> 00:00:59,770
and if we scroll down a little bit, there's a diagram here

21
00:00:59,770 --> 00:01:01,510
that actually shows that.

22
00:01:01,510 --> 00:01:04,080
This is an example of how that would work.

23
00:01:04,080 --> 00:01:07,660
So on the left is a tablet display showing two fragments,

24
00:01:07,660 --> 00:01:09,990
that is displayed in the same activity,

25
00:01:09,990 --> 00:01:13,040
in fact that's very similar to what we're going to produce.

26
00:01:13,040 --> 00:01:16,100
When you edit a task from the recycler due list,

27
00:01:16,100 --> 00:01:18,680
the task details will appear on the right hand side

28
00:01:18,680 --> 00:01:19,830
of the screen.

29
00:01:19,830 --> 00:01:23,380
So the diagram on the right shows the same two fragments

30
00:01:23,380 --> 00:01:26,770
being used on a phone in portrait mode.

31
00:01:26,770 --> 00:01:28,557
So this uses the same two fragments,

32
00:01:28,557 --> 00:01:30,730
that's the tabulate example

33
00:01:30,730 --> 00:01:32,770
but because the screen isn't wide enough

34
00:01:32,770 --> 00:01:34,810
to show them both side by side,

35
00:01:34,810 --> 00:01:38,240
each fragment is displayed in a separate activity.

36
00:01:38,240 --> 00:01:40,990
In fact we're going to do things slightly differently.

37
00:01:40,990 --> 00:01:43,300
So launching a new activity is fine,

38
00:01:43,300 --> 00:01:45,560
if all you're doing is displaying data.

39
00:01:45,560 --> 00:01:48,420
When you allow the right hand fragment to edit the data,

40
00:01:48,420 --> 00:01:50,170
things get difficult.

41
00:01:50,170 --> 00:01:53,020
The Google's example only show displaying data

42
00:01:53,020 --> 00:01:55,870
and using two separate activities in portrait mode.

43
00:01:55,870 --> 00:01:58,200
We're gonna swap out the fragments instead.

44
00:01:58,200 --> 00:02:00,280
So let's see how that works.

45
00:02:00,280 --> 00:02:02,160
What we're gonna do now is take it slow.

46
00:02:02,160 --> 00:02:04,740
We're gonna start by getting a single fragment working

47
00:02:04,740 --> 00:02:07,300
in portrait mode on a phone.

48
00:02:07,300 --> 00:02:09,240
Once we've seen how to use a fragment,

49
00:02:09,240 --> 00:02:12,880
we'll build on that to handle the side by side display

50
00:02:12,880 --> 00:02:16,060
on devices that have enough screen space.

51
00:02:16,060 --> 00:02:18,230
So switching back to Android studio,

52
00:02:18,230 --> 00:02:21,410
we're gonna start off by creating a new fragment.

53
00:02:21,410 --> 00:02:23,200
So I'm gonna right click on the package name,

54
00:02:23,200 --> 00:02:26,090
new, just now we're gonna come down to fragment,

55
00:02:26,090 --> 00:02:27,510
select that from the menu,

56
00:02:27,510 --> 00:02:29,743
then choose Fragment (Blank) from the menu.

57
00:02:31,090 --> 00:02:33,347
We're gonna call this one AddEditFragment.

58
00:02:35,539 --> 00:02:37,012
And now because we've already got a layout

59
00:02:37,012 --> 00:02:40,030
called fragment_add_edit,

60
00:02:40,030 --> 00:02:42,549
Android Studios wants to call this layout

61
00:02:42,549 --> 00:02:44,132
fragment_add_edit2.

62
00:02:45,480 --> 00:02:49,110
So we could just untick the create layout XML box

63
00:02:49,110 --> 00:02:50,980
but that just doesn't create the layout,

64
00:02:50,980 --> 00:02:53,090
it also adds a bit of code to use.

65
00:02:53,090 --> 00:02:55,270
So we're gonna create this second layout file

66
00:02:55,270 --> 00:02:56,690
so it adds that code

67
00:02:56,690 --> 00:02:59,640
then we'll delete this extra layout later.

68
00:02:59,640 --> 00:03:02,920
We do want both of these other boxes to be checked though.

69
00:03:02,920 --> 00:03:06,520
They generate a lot of code for us which saves us time.

70
00:03:06,520 --> 00:03:08,270
And obviously remember in the case

71
00:03:08,270 --> 00:03:10,180
of the case of the course language drop down,

72
00:03:10,180 --> 00:03:11,600
make sure that's set to Kotlin

73
00:03:11,600 --> 00:03:13,100
and I'm gonna click on finish.

74
00:03:16,110 --> 00:03:18,410
Okay, so you can see Android Studios generated

75
00:03:18,410 --> 00:03:20,330
quite a lot of code for us.

76
00:03:20,330 --> 00:03:22,900
So let's work through it and make some changes.

77
00:03:22,900 --> 00:03:24,850
So the generic bits of the code are all marked

78
00:03:24,850 --> 00:03:26,880
with TODO comments, as you can see here on line 12

79
00:03:26,880 --> 00:03:28,187
and another one on line 27

80
00:03:28,187 --> 00:03:29,780
and that makes them easy to spot

81
00:03:29,780 --> 00:03:32,940
because they're the parts of the code we need to change.

82
00:03:32,940 --> 00:03:36,010
So the first change is to the arguments that we're gonna

83
00:03:36,010 --> 00:03:38,760
pass to the fragment in a bundle.

84
00:03:38,760 --> 00:03:40,990
Now we'll only be passing a single argument

85
00:03:40,990 --> 00:03:42,970
which will be the task to edit.

86
00:03:42,970 --> 00:03:43,803
So what we're gonna do is

87
00:03:43,803 --> 00:03:47,040
refactor the ARG_PARAM1 constant

88
00:03:47,040 --> 00:03:50,033
to ARG_TASK and then delete ARG_PARAM2.

89
00:03:51,451 --> 00:03:52,690
Just go ahead and do that.

90
00:03:52,690 --> 00:03:54,380
I'm gonna refactor that.

91
00:03:54,380 --> 00:03:55,720
Rename it.

92
00:03:55,720 --> 00:03:57,370
And we're gonna call it ARG_TASK.

93
00:04:01,685 --> 00:04:05,102
And we're gonna change the param1 to task

94
00:04:06,349 --> 00:04:09,420
and for the ARG_PARAM2, I mentioned we don't need that

95
00:04:09,420 --> 00:04:10,570
so I'm gonna delete it.

96
00:04:11,700 --> 00:04:14,180
Once that's done, we can delete the TODO line as well,

97
00:04:14,180 --> 00:04:16,230
'cause we've actually made those changes.

98
00:04:17,170 --> 00:04:18,880
Alright so the class itself contains

99
00:04:18,880 --> 00:04:20,519
fields for the arguments,

100
00:04:20,519 --> 00:04:22,670
so once again, we need to actually go ahead

101
00:04:22,670 --> 00:04:24,450
and make some changes to that.

102
00:04:24,450 --> 00:04:26,710
So I'm gonna refactor the first one to be called task,

103
00:04:26,710 --> 00:04:28,233
this param1, let's do that.

104
00:04:30,720 --> 00:04:33,793
This param1, we'll change that to be task.

105
00:04:36,080 --> 00:04:38,740
And we'll also change the definition,

106
00:04:38,740 --> 00:04:41,227
which is a string, to a task,

107
00:04:41,227 --> 00:04:43,370
leaving the question mark there.

108
00:04:43,370 --> 00:04:47,300
And we're also then going to delete the second one,

109
00:04:47,300 --> 00:04:49,693
the second parameter, because again we're not using that.

110
00:04:50,710 --> 00:04:52,530
Now we are gonna come back to this line.

111
00:04:52,530 --> 00:04:54,350
For now though, we're seeing the basic edits

112
00:04:54,350 --> 00:04:56,150
that we need to make to the code that

113
00:04:56,150 --> 00:04:58,170
Android Studios generated for us.

114
00:04:58,170 --> 00:05:00,033
So I'm gonna delete the TODO for this as well

115
00:05:00,033 --> 00:05:02,480
because we have made the changes to that.

116
00:05:02,480 --> 00:05:04,610
So scrolling down a little bit now.

117
00:05:04,610 --> 00:05:05,490
We've got a few errors.

118
00:05:05,490 --> 00:05:08,780
Basically we have a param2 is referencing the code,

119
00:05:08,780 --> 00:05:11,250
So I'm just gonna delete those lines as we come to them.

120
00:05:11,250 --> 00:05:13,600
So on the onCreate, I go ahead delete that now.

121
00:05:14,790 --> 00:05:16,360
Now this code looks a bit strange now,

122
00:05:16,360 --> 00:05:18,880
the code that's starting on line 30.

123
00:05:18,880 --> 00:05:21,170
It makes sense to use LET when dealing

124
00:05:21,170 --> 00:05:22,760
with more than one parameter

125
00:05:22,760 --> 00:05:24,640
but now there's only one,

126
00:05:24,640 --> 00:05:27,430
we can just use a safe call operator instead.

127
00:05:27,430 --> 00:05:28,750
So that will still work,

128
00:05:28,750 --> 00:05:30,210
it's just unnecessary.

129
00:05:30,210 --> 00:05:32,150
So let's go ahead and make that change.

130
00:05:32,150 --> 00:05:34,475
So I'm going to leave the super.onCreate

131
00:05:34,475 --> 00:05:37,980
and just type in task equals arguments

132
00:05:37,980 --> 00:05:40,200
question mark because this is a safe call operator,

133
00:05:40,200 --> 00:05:42,230
dot, and we're gonna leave getString in there

134
00:05:42,230 --> 00:05:43,063
for a moment,

135
00:05:44,630 --> 00:05:45,463
as ARG_TASK.

136
00:05:46,777 --> 00:05:48,946
That'd be the replacement code.

137
00:05:48,946 --> 00:05:50,470
And I'll delete the other code out.

138
00:05:50,470 --> 00:05:52,410
Now, we've still got an error here.

139
00:05:52,410 --> 00:05:55,640
That's because we changed the top of task to be a task,

140
00:05:55,640 --> 00:05:58,800
but the generator code and the code that I retyped in,

141
00:05:58,800 --> 00:06:01,310
is still using getString to try and retrieve it

142
00:06:01,310 --> 00:06:02,360
from the bundle.

143
00:06:02,360 --> 00:06:06,347
So we need to change this getString to getParcelable instead

144
00:06:06,347 --> 00:06:08,360
and that's obviously because we've implemented

145
00:06:08,360 --> 00:06:12,190
the task class to basically implement

146
00:06:12,190 --> 00:06:14,220
the passable interface.

147
00:06:14,220 --> 00:06:16,890
So I'm gonna do arguments question mark dot,

148
00:06:16,890 --> 00:06:18,567
and it's gonna be getPARCELABLE

149
00:06:20,310 --> 00:06:23,180
and in parentheses, ARG_TASK, but before that

150
00:06:23,180 --> 00:06:25,860
we need to use a diamond operator there

151
00:06:25,860 --> 00:06:30,060
and put Task within that,

152
00:06:30,060 --> 00:06:32,220
to specify the top of object that we're expecting

153
00:06:32,220 --> 00:06:34,000
to get from the bundle.

154
00:06:34,000 --> 00:06:36,110
And when I do that, the error disappears.

155
00:06:36,110 --> 00:06:37,670
And if I just go ahead and delete the task,

156
00:06:37,670 --> 00:06:40,210
there you can see there remove explicit type arguments.

157
00:06:40,210 --> 00:06:41,510
I can actually remove that

158
00:06:43,070 --> 00:06:44,550
because it's now optional.

159
00:06:44,550 --> 00:06:47,820
Basically, conversion 1.2.50 of Kotlin,

160
00:06:47,820 --> 00:06:50,990
There's no log other requirement for the top to be specified

161
00:06:50,990 --> 00:06:52,800
so if you get a message like I did,

162
00:06:52,800 --> 00:06:54,443
to remove explicit top arguments,

163
00:06:54,443 --> 00:06:57,220
then you can delete the diamond operator and task

164
00:06:57,220 --> 00:06:58,690
after getParcelable.

165
00:06:58,690 --> 00:07:00,090
So I get up out and do that.

166
00:07:02,507 --> 00:07:03,830
And if I click in there again, you can see

167
00:07:03,830 --> 00:07:06,300
I've got this warning, remove explicit type arguments,

168
00:07:06,300 --> 00:07:08,320
so for that reason I'm going to remove it.

169
00:07:08,320 --> 00:07:09,760
But obviously, if you haven't got that,

170
00:07:09,760 --> 00:07:10,860
you can leave it in.

171
00:07:10,860 --> 00:07:14,080
Alright, so now moving on down to the onCreate view function

172
00:07:14,080 --> 00:07:16,440
that we've got here starting on line 33,

173
00:07:16,440 --> 00:07:20,670
that's inflating the fragment_add_edit2 layout

174
00:07:20,670 --> 00:07:22,960
that Android Studios generated for us.

175
00:07:22,960 --> 00:07:24,270
Now, we've already got a layout

176
00:07:24,270 --> 00:07:26,420
which we created in previous videos.

177
00:07:26,420 --> 00:07:29,100
So for that reason, we can go now ahead and

178
00:07:29,100 --> 00:07:30,430
make a change to that and inflate

179
00:07:30,430 --> 00:07:33,000
that one but changing the 2,

180
00:07:33,000 --> 00:07:37,310
we'll now be inflating the layout that we created earlier.

181
00:07:37,310 --> 00:07:40,410
And we can now also delete the generated layout,

182
00:07:40,410 --> 00:07:44,980
the fragment_add_edit2 in the res layout folder.

183
00:07:44,980 --> 00:07:46,550
Just gonna go ahead and do that.

184
00:07:46,550 --> 00:07:49,150
And that of course was the one that we created in this video

185
00:07:49,150 --> 00:07:50,680
so make sure you delete the right one.

186
00:07:50,680 --> 00:07:54,157
I'm gonna select delete there, click on OK.

187
00:07:54,157 --> 00:07:55,557
And that's now been removed.

188
00:07:56,560 --> 00:07:59,110
And again, the reason we told Android Studios to generate

189
00:07:59,110 --> 00:08:01,520
a layout even though we already had one,

190
00:08:01,520 --> 00:08:02,830
was so that it would put the line

191
00:08:02,830 --> 00:08:05,150
to inflate the layout code in there for us,

192
00:08:05,150 --> 00:08:07,400
to allow us to make a simple change.

193
00:08:07,400 --> 00:08:09,750
So we could've told it not to create a new layout,

194
00:08:09,750 --> 00:08:12,230
but then we'd have to remember to type that line in,

195
00:08:12,230 --> 00:08:15,630
changing it and deleting the other layout is just easier.

196
00:08:15,630 --> 00:08:17,070
Right, so next we have the function

197
00:08:17,070 --> 00:08:19,580
that the fragments button will call.

198
00:08:19,580 --> 00:08:21,990
This is at the moment called onButtonPressed

199
00:08:21,990 --> 00:08:23,410
and it's got a TODO there.

200
00:08:23,410 --> 00:08:26,330
So Android Studios is giving this generic code here.

201
00:08:26,330 --> 00:08:28,580
It's quite likely that we'll want the fragment

202
00:08:28,580 --> 00:08:31,860
to notify its activity when something happens.

203
00:08:31,860 --> 00:08:32,972
And that's why Android Studios generated

204
00:08:32,972 --> 00:08:35,419
this onButtonPressed function,

205
00:08:35,419 --> 00:08:39,960
and it's also created a general purpose parameter at uri.

206
00:08:39,960 --> 00:08:42,169
Now, we will have a button on this fragment,

207
00:08:42,169 --> 00:08:45,120
the save button that we created in the layout.

208
00:08:45,120 --> 00:08:46,260
But what we're gonna do is attach

209
00:08:46,260 --> 00:08:48,230
an onSaveClickedListeners to it later

210
00:08:48,230 --> 00:08:50,350
and for that reason we don't need this function.

211
00:08:50,350 --> 00:08:53,690
So I'm gonna delete this onPressed function altogether

212
00:08:53,690 --> 00:08:55,040
including the TODO comment.

213
00:08:57,050 --> 00:08:59,190
Now, the onAttach function here,

214
00:08:59,190 --> 00:09:01,710
that's checking that our activity does implement

215
00:09:01,710 --> 00:09:03,370
the necessary interface,

216
00:09:03,370 --> 00:09:04,950
in this case the interface that allows

217
00:09:04,950 --> 00:09:06,840
our fragment to call it back.

218
00:09:06,840 --> 00:09:08,170
Now that name that it's using here,

219
00:09:08,170 --> 00:09:12,610
this OnFragmentInteractionListener is a general purpose name

220
00:09:12,610 --> 00:09:14,880
and frankly is a bit of a mouthful.

221
00:09:14,880 --> 00:09:16,770
So I'm gonna refactor and rename it

222
00:09:16,770 --> 00:09:19,020
and change it to OnSaveClicked

223
00:09:19,020 --> 00:09:21,920
And I'm gonna do that by renaming the interface down here.

224
00:09:24,320 --> 00:09:26,440
Refactor, rename, and then I'm gonna

225
00:09:26,440 --> 00:09:28,693
go with the name there of OnSaveClicked.

226
00:09:34,475 --> 00:09:37,345
And I'm just gonna click OK there.

227
00:09:37,345 --> 00:09:40,985
And then I'm gonna click on Do Refactor.

228
00:09:40,985 --> 00:09:43,490
And you can see it's made the necessary change to the call

229
00:09:43,490 --> 00:09:47,723
on line 41 as well as the interface itself on line 64.

230
00:09:48,600 --> 00:09:50,750
So Android Studios helpfully changes references

231
00:09:50,750 --> 00:09:53,120
in the comments in the code itself,

232
00:09:53,120 --> 00:09:55,780
when you click on that Do Refactor which is pretty handy.

233
00:09:55,780 --> 00:09:56,930
Alright, moving on again now though,

234
00:09:56,930 --> 00:10:00,380
after the touch function, we have the onDetach function,

235
00:10:00,380 --> 00:10:02,710
and that just sets the list the to know,

236
00:10:02,710 --> 00:10:05,390
and I'm gonna be explaining all this fragment lifecycle

237
00:10:05,390 --> 00:10:07,640
functions in the next few videos

238
00:10:07,640 --> 00:10:09,840
so don't worry about what it's doing there,

239
00:10:09,840 --> 00:10:10,960
just at the moment.

240
00:10:10,960 --> 00:10:14,250
Next time we got another TODO here within our interface

241
00:10:14,250 --> 00:10:16,530
and we have already named the interface.

242
00:10:16,530 --> 00:10:18,130
But I'm also gonna take the opportunity

243
00:10:18,130 --> 00:10:20,510
to refactor the actual function name

244
00:10:20,510 --> 00:10:23,270
which is currently set to onFragmentInteraction.

245
00:10:23,270 --> 00:10:24,620
Let's go ahead and do that.

246
00:10:26,830 --> 00:10:28,740
Now, our activity's gonna be responding

247
00:10:28,740 --> 00:10:30,290
when the save button is clicked,

248
00:10:30,290 --> 00:10:33,170
so it makes sense to call this function onSaveClicked.

249
00:10:37,270 --> 00:10:39,650
And obviously the function has a lowercase O,

250
00:10:39,650 --> 00:10:41,351
and the interface has a capital O

251
00:10:41,351 --> 00:10:42,790
as you can see there.

252
00:10:42,790 --> 00:10:44,410
Now that's a common way of naming things,

253
00:10:44,410 --> 00:10:46,570
and we've seen it with the OnClickedListeners

254
00:10:46,570 --> 00:10:48,180
that we've written previously.

255
00:10:48,180 --> 00:10:50,920
Now we also have to remove the parameter

256
00:10:50,920 --> 00:10:53,450
because we won't be passing back to the activity.

257
00:10:53,450 --> 00:10:54,283
Gonna do that.

258
00:10:55,500 --> 00:10:57,240
And I'm also gonna take the opportunity to delete

259
00:10:57,240 --> 00:11:01,050
the TODO comment since we've now made the required changes.

260
00:11:01,050 --> 00:11:03,800
Right, so the final part of generic code here

261
00:11:03,800 --> 00:11:05,760
is the companion object.

262
00:11:05,760 --> 00:11:08,680
So this provides a static method in Java terms

263
00:11:08,680 --> 00:11:10,870
that we can use to create new instances

264
00:11:10,870 --> 00:11:12,020
of the fragment.

265
00:11:12,020 --> 00:11:14,200
So it uses the same two parameters

266
00:11:14,200 --> 00:11:16,270
and obviously we need to change it to use our

267
00:11:16,270 --> 00:11:18,470
single task parameter.

268
00:11:18,470 --> 00:11:20,540
Let's go ahead and make some changes to that.

269
00:11:20,540 --> 00:11:24,837
So firstly on the newInstance function declaration,

270
00:11:24,837 --> 00:11:28,120
gonna change that now so it's just got the one.

271
00:11:28,120 --> 00:11:32,533
It's gonna be task colon task question mark.

272
00:11:35,110 --> 00:11:36,820
So what I'll do is just delete those two lines there,

273
00:11:36,820 --> 00:11:38,570
the two puts because we're gonna be

274
00:11:39,661 --> 00:11:41,730
putParcelable instead.

275
00:11:41,730 --> 00:11:45,069
So it's gonna be putParcelable

276
00:11:45,069 --> 00:11:48,380
and it's gonna be ARG_TASK, task

277
00:11:49,320 --> 00:11:51,140
and you can see that I've deleted the second layer

278
00:11:51,140 --> 00:11:52,810
second entry for ARG_PARAM2

279
00:11:52,810 --> 00:11:56,510
because we're only using a single parameter here now.

280
00:11:56,510 --> 00:11:59,590
So this newInstance function provides a convenient way

281
00:11:59,590 --> 00:12:01,960
to pass a task to the fragment

282
00:12:01,960 --> 00:12:04,926
without having to include all that bundle code

283
00:12:04,926 --> 00:12:06,510
in our calling code.

284
00:12:06,510 --> 00:12:08,800
Now, if we didn't use the newInstance function,

285
00:12:08,800 --> 00:12:10,330
our calling code would look something like

286
00:12:10,330 --> 00:12:12,110
the code that I'm about to paste in.

287
00:12:12,110 --> 00:12:14,702
I'll paste this in right at the end of the file down here

288
00:12:14,702 --> 00:12:17,480
so we don't get any errors.

289
00:12:17,480 --> 00:12:19,100
We are going to comment this out.

290
00:12:19,100 --> 00:12:20,193
So that would be our calling code

291
00:12:20,193 --> 00:12:22,320
what it would normally look like.

292
00:12:22,320 --> 00:12:24,700
And we could simplify that a little bit

293
00:12:24,700 --> 00:12:27,620
by using the apply function from the AddEditFragment

294
00:12:27,620 --> 00:12:28,730
in the bundle.

295
00:12:28,730 --> 00:12:29,920
So we can make it a little bit simpler,

296
00:12:29,920 --> 00:12:31,367
something along the lines of this,

297
00:12:31,367 --> 00:12:33,053
and I'll paste this code as well.

298
00:12:35,804 --> 00:12:37,230
So that's making it a little bit simpler.

299
00:12:37,230 --> 00:12:39,730
But I think in reality it's a lot simpler

300
00:12:39,730 --> 00:12:42,270
to use what we've got with this newInstance function.

301
00:12:42,270 --> 00:12:44,640
This is effectively what we're going to be using.

302
00:12:44,640 --> 00:12:47,070
So I think you'd agree that's a lot simpler.

303
00:12:47,070 --> 00:12:49,097
We don't have to remember all about bundles and arguments,

304
00:12:49,097 --> 00:12:52,460
but we wanna create a new AddEditFragment.

305
00:12:52,460 --> 00:12:53,600
Now, you might be wondering there why

306
00:12:53,600 --> 00:12:56,900
we didn't just pass the task argument in the constructor.

307
00:12:56,900 --> 00:12:58,540
That would be the easiest of all,

308
00:12:58,540 --> 00:13:01,250
but unfortunately it's not possible.

309
00:13:01,250 --> 00:13:03,920
Fragments must have a parameter less constructor,

310
00:13:03,920 --> 00:13:06,780
in order for Android to create them when it needs to.

311
00:13:06,780 --> 00:13:09,100
Now remember that Android does all sorts of things

312
00:13:09,100 --> 00:13:10,610
behind the scenes.

313
00:13:10,610 --> 00:13:12,690
If our fragment gets destroyed,

314
00:13:12,690 --> 00:13:14,090
Android will recreate it.

315
00:13:14,090 --> 00:13:16,600
So it takes care of saving the fragment state for us,

316
00:13:16,600 --> 00:13:19,010
but it has no idea what arguments we may have passed

317
00:13:19,010 --> 00:13:20,330
when we created it.

318
00:13:20,330 --> 00:13:23,040
If we go passing arguments to the fragment constructor,

319
00:13:23,040 --> 00:13:26,300
they would be available when Android recreates the fragment.

320
00:13:26,300 --> 00:13:29,480
So that's why arguments are passed in the arguments bundle,

321
00:13:29,480 --> 00:13:32,010
and why we can't pass them in the constructor.

322
00:13:32,010 --> 00:13:33,560
Alright, so what I'm gonna do is comment out

323
00:13:33,560 --> 00:13:37,170
these three functions so that they are available

324
00:13:37,170 --> 00:13:40,540
in the resources if you wanna check out little tune edits.

325
00:13:40,540 --> 00:13:43,350
Alright, so let's finish off the changes

326
00:13:43,350 --> 00:13:45,070
before reviewing what we've done.

327
00:13:45,070 --> 00:13:46,690
Now I haven't deleted the TODO item

328
00:13:46,690 --> 00:13:48,540
in the companion object yet.

329
00:13:48,540 --> 00:13:51,290
That's because there is still a minor change to make.

330
00:13:51,290 --> 00:13:54,153
Android Studios is actually giving us a few warnings here.

331
00:13:56,540 --> 00:13:57,950
Now, the first one if you hover over here

332
00:13:57,950 --> 00:14:00,980
it's saying that function onsaveClicked is never used,

333
00:14:00,980 --> 00:14:02,420
so we can ignore that one.

334
00:14:02,420 --> 00:14:05,140
But the next two are interesting, we'll hover down here.

335
00:14:05,140 --> 00:14:06,370
So you can see that they're complaining about

336
00:14:06,370 --> 00:14:09,960
the names that we've got in the companion objects comments.

337
00:14:09,960 --> 00:14:12,780
And you can see we've got param1 and param2.

338
00:14:12,780 --> 00:14:14,780
When you format comments like this, like we've done

339
00:14:14,780 --> 00:14:17,040
or like what was created automatically for us

340
00:14:17,040 --> 00:14:19,590
from Android Studios, starting on line 68,

341
00:14:19,590 --> 00:14:21,830
they're not treated as ordinary comments.

342
00:14:21,830 --> 00:14:24,160
They're actually Java dot comments and they're used to

343
00:14:24,160 --> 00:14:26,340
automatically generate documentation

344
00:14:26,340 --> 00:14:28,300
for our classes and functions.

345
00:14:28,300 --> 00:14:30,020
Now Android Studios tries to help us

346
00:14:30,020 --> 00:14:32,160
make sure they're accurate and in this case,

347
00:14:32,160 --> 00:14:36,120
to check the names param1 and param2 against our code,

348
00:14:36,120 --> 00:14:39,180
we no longer have a param2, and we've renamed

349
00:14:39,180 --> 00:14:42,560
param1 to task, so therefore we need to update the comment.

350
00:14:42,560 --> 00:14:44,020
So go ahead and do that.

351
00:14:44,020 --> 00:14:45,920
Delete the second entry first, param2.

352
00:14:46,860 --> 00:14:49,520
And for param1, I'm gonna change that

353
00:14:49,520 --> 00:14:53,130
to make it task and I'm gonna call it a description.

354
00:14:53,130 --> 00:14:57,690
I can put task to be edited or null to add new task.

355
00:14:57,690 --> 00:15:00,303
Or null to add a new task.

356
00:15:02,471 --> 00:15:03,950
And you can see those warnings are now disappeared.

357
00:15:03,950 --> 00:15:05,480
We've only got the other warning there

358
00:15:05,480 --> 00:15:09,140
about the fact that onSaveClicked has never been used.

359
00:15:09,140 --> 00:15:12,340
Now that I've done that, I can delete the TODO there.

360
00:15:12,340 --> 00:15:13,990
Alright, so I'm gonna finish this video

361
00:15:13,990 --> 00:15:16,330
just by reviewing what we've just done.

362
00:15:16,330 --> 00:15:18,201
So Android Studios has generated a lot of

363
00:15:18,201 --> 00:15:21,740
generic code for us when the created the new fragment.

364
00:15:21,740 --> 00:15:24,250
Not all of it's needed in all situations.

365
00:15:24,250 --> 00:15:25,860
And things like the parameter names

366
00:15:25,860 --> 00:15:29,000
and the namings of the interface are just placeholders.

367
00:15:29,000 --> 00:15:31,830
We were expected to rename them to something more meaningful

368
00:15:31,830 --> 00:15:33,830
for our application, which is, of course,

369
00:15:33,830 --> 00:15:35,000
what we've done here.

370
00:15:35,000 --> 00:15:37,740
Now we had to delete all references to param2,

371
00:15:37,740 --> 00:15:39,520
'cause we're only passing a single argument

372
00:15:39,520 --> 00:15:41,840
to the fragment, the task to be edited.

373
00:15:41,840 --> 00:15:43,980
Now, we could've left it with the name param1,

374
00:15:43,980 --> 00:15:45,570
but that's not very readable.

375
00:15:45,570 --> 00:15:47,932
And we also had to change the parameter type,

376
00:15:47,932 --> 00:15:50,640
and that's because we're passing a task and not a string.

377
00:15:50,640 --> 00:15:53,150
So we've taken a generic fragment class

378
00:15:53,150 --> 00:15:56,910
that Android Studios created and tailored it slightly.

379
00:15:56,910 --> 00:15:59,723
So looking at the files in the project pane,

380
00:16:01,460 --> 00:16:03,780
we've got MainActivity.kt,

381
00:16:03,780 --> 00:16:06,480
that uses the layout activity that is called main,

382
00:16:06,480 --> 00:16:08,567
which includes content_main.

383
00:16:08,567 --> 00:16:11,600
Now that's just a convenience to keep the action bar

384
00:16:11,600 --> 00:16:14,690
and floating action button separate from our widgets.

385
00:16:14,690 --> 00:16:17,537
The widgets we want to display go in content_main.

386
00:16:18,625 --> 00:16:21,940
Now content_main in this app doesn't contain any widgets.

387
00:16:21,940 --> 00:16:24,120
We've just used a fragment instead.

388
00:16:24,120 --> 00:16:26,193
So if we open up content_main now,

389
00:16:28,300 --> 00:16:30,398
close the project pane briefly.

390
00:16:30,398 --> 00:16:32,574
And you see that we have here there's a fragment

391
00:16:32,574 --> 00:16:35,407
that's set to MainActivityFragment

392
00:16:36,538 --> 00:16:39,110
And you can see they have it at set to MainActivityFragment.

393
00:16:39,110 --> 00:16:41,910
We're going to the text code or I could just go

394
00:16:41,910 --> 00:16:43,310
to the name down there, MainActivityFragment

395
00:16:43,310 --> 00:16:45,030
under the fragment name.

396
00:16:45,030 --> 00:16:46,770
So when we ran the app earlier,

397
00:16:46,770 --> 00:16:50,370
MainActivityFragment used the Fragment_Main layout

398
00:16:50,370 --> 00:16:52,390
to display our recycler view.

399
00:16:52,390 --> 00:16:54,240
What we're gonna do next is add the code

400
00:16:54,240 --> 00:16:57,840
to swap MainActivityFragment for AddEditFragment.

401
00:16:57,840 --> 00:17:00,160
We'll do that when the edit button's tapped,

402
00:17:00,160 --> 00:17:02,640
so that the display will show the edit screen,

403
00:17:02,640 --> 00:17:04,839
instead of the recycler view list.

404
00:17:04,839 --> 00:17:06,690
When the user finishes editing,

405
00:17:06,690 --> 00:17:08,550
we'll swap the fragments back.

406
00:17:08,550 --> 00:17:10,770
So AddEditFragment will be removed,

407
00:17:10,770 --> 00:17:14,030
and then MainActivityFragment will appear in its place.

408
00:17:14,030 --> 00:17:15,839
But I wanna make one more quick change,

409
00:17:15,839 --> 00:17:19,400
which is to add logging to our four fragment functions.

410
00:17:19,400 --> 00:17:21,750
Let's go back to our file,

411
00:17:21,750 --> 00:17:24,030
in this case the AddEditFragment.

412
00:17:24,030 --> 00:17:26,339
So we need to first add our usual logging tag

413
00:17:26,339 --> 00:17:27,720
at the top of the file.

414
00:17:27,720 --> 00:17:32,660
And I'll add that to above the ARG_TASK setting.

415
00:17:32,660 --> 00:17:34,860
So it's gonna be, or definition, I should say,

416
00:17:34,860 --> 00:17:39,860
private const val_TAG is equal to AddEditFragment.

417
00:17:44,060 --> 00:17:45,290
The actual logging itself,

418
00:17:45,290 --> 00:17:49,401
we'll start by adding one in the onCreate method,

419
00:17:49,401 --> 00:17:50,980
onCreate function.

420
00:17:50,980 --> 00:17:53,600
We'll have that as the first line before the call to super.

421
00:17:53,600 --> 00:17:57,630
So Log.d parentheses TAG comma,

422
00:17:57,630 --> 00:18:01,103
double quote onCreate colon starts.

423
00:18:02,840 --> 00:18:04,720
Just gonna take a copy of that to be easier

424
00:18:04,720 --> 00:18:07,470
to change the other definitions that we add.

425
00:18:07,470 --> 00:18:10,270
OnCreateView, I'm gonna paste that in as the first line.

426
00:18:11,940 --> 00:18:13,633
And we'll change this to onCreateView.

427
00:18:16,600 --> 00:18:18,943
Next onAttach, let's add one for that.

428
00:18:23,870 --> 00:18:26,060
OnAttach starts,

429
00:18:26,060 --> 00:18:28,693
and finally we're gonna add one to our onDetach.

430
00:18:37,330 --> 00:18:38,683
OnDetach starts.

431
00:18:41,430 --> 00:18:43,050
So now that we've created our fragment,

432
00:18:43,050 --> 00:18:45,660
we're gonna write the code to swap it in and out,

433
00:18:45,660 --> 00:18:46,890
in the next video.

434
00:18:46,890 --> 00:18:48,370
And once you've seen that working,

435
00:18:48,370 --> 00:18:50,660
we'll then have a look at the fragment lifecycle.

436
00:18:50,660 --> 00:18:52,410
So, I'll see you in the next video.

