1
00:00:00,087 --> 00:00:02,670
(upbeat music)

2
00:00:05,541 --> 00:00:06,528
Alright, so we added two

3
00:00:06,528 --> 00:00:08,601
more menu items in the last video.

4
00:00:08,601 --> 00:00:09,997
Now we need to write the code

5
00:00:09,997 --> 00:00:12,395
to make these menu items work.

6
00:00:12,395 --> 00:00:14,021
So we're going to go back to main activities,

7
00:00:14,021 --> 00:00:16,113
I'm going to close down this design.

8
00:00:16,113 --> 00:00:18,334
The feed on manual XML

9
00:00:18,334 --> 00:00:20,052
(mouse clicks)

10
00:00:20,052 --> 00:00:21,092
Go to my main activity.

11
00:00:21,092 --> 00:00:22,558
Bring it on the screen,

12
00:00:22,558 --> 00:00:23,883
And we need to add the new items

13
00:00:23,883 --> 00:00:27,383
in the on options items selected function.

14
00:00:29,166 --> 00:00:30,635
This one here.

15
00:00:30,635 --> 00:00:32,813
Now this takes a bit of thought,

16
00:00:32,813 --> 00:00:34,097
because these two options are going to

17
00:00:34,097 --> 00:00:36,975
keep most of the euro unchanged,

18
00:00:36,975 --> 00:00:39,338
they just alter the limit perimeter.

19
00:00:39,338 --> 00:00:40,416
So that means we have to store

20
00:00:40,416 --> 00:00:41,963
the base part of the yearout

21
00:00:41,963 --> 00:00:43,281
as a class variable

22
00:00:43,281 --> 00:00:45,896
rather than a private variable.

23
00:00:45,896 --> 00:00:47,455
Now at the moment,

24
00:00:47,455 --> 00:00:49,448
we've got to feedUrl

25
00:00:49,448 --> 00:00:52,551
private in the onOptionsItemSelected method.

26
00:00:52,551 --> 00:00:54,154
So we need to move it into the class

27
00:00:54,154 --> 00:00:55,214
so we that we can use it's value

28
00:00:55,214 --> 00:00:56,897
from one quarter to the next

29
00:00:56,897 --> 00:00:58,561
as different limits are chosen.

30
00:00:58,561 --> 00:01:00,693
And we also need to store the current limit size,

31
00:01:00,693 --> 00:01:02,316
so I'm going to add two variables

32
00:01:02,316 --> 00:01:04,280
to the top of the class.

33
00:01:04,280 --> 00:01:05,364
So what I'm going to do also is

34
00:01:05,364 --> 00:01:07,793
just take a copy of that

35
00:01:07,793 --> 00:01:08,626
feedUrl.

36
00:01:09,912 --> 00:01:11,340
Well actually I'll cut it out of there,

37
00:01:11,340 --> 00:01:14,509
Go back up to the top of the class,

38
00:01:14,509 --> 00:01:16,229
and just below the tag,

39
00:01:16,229 --> 00:01:19,396
you're going to put the code in there.

40
00:01:20,781 --> 00:01:22,076
Tidy this up a little bit,

41
00:01:22,076 --> 00:01:24,597
I'll put it below the download data,

42
00:01:24,597 --> 00:01:25,430
So instead of just val now

43
00:01:25,430 --> 00:01:27,065
it's going to be private.

44
00:01:27,065 --> 00:01:28,918
(typing)

45
00:01:28,918 --> 00:01:30,835
It's going to be a ver,

46
00:01:32,283 --> 00:01:34,384
equals, and we're going to get the

47
00:01:34,384 --> 00:01:37,380
default value in a minute.

48
00:01:37,380 --> 00:01:39,349
For the next line we need to add this feed limit

49
00:01:39,349 --> 00:01:41,807
so I'm going to set that to private var

50
00:01:41,807 --> 00:01:44,057
feedlimit = 10.

51
00:01:45,044 --> 00:01:47,300
Alright so then we're going to take a copy,

52
00:01:47,300 --> 00:01:49,983
of this line at the top of the application

53
00:01:49,983 --> 00:01:52,483
We're just going to copy that.

54
00:01:54,138 --> 00:01:56,638
Let's paste it in our feedUrl.

55
00:01:58,003 --> 00:02:00,607
But, instead of where it's got 10,

56
00:02:00,607 --> 00:02:02,840
you'll change that to percent d.

57
00:02:02,840 --> 00:02:04,906
That's a lower case d.

58
00:02:04,906 --> 00:02:07,637
Now, this is a way of specifying an integer value

59
00:02:07,637 --> 00:02:10,405
that's going to be replaced by an actual value

60
00:02:10,405 --> 00:02:12,755
using a string dot format function.

61
00:02:12,755 --> 00:02:14,787
Now, the string dot format function takes a string

62
00:02:14,787 --> 00:02:17,743
containing special format codes like we said d

63
00:02:17,743 --> 00:02:19,423
and a number of values that are used

64
00:02:19,423 --> 00:02:21,527
to replace the format codes.

65
00:02:21,527 --> 00:02:22,360
You can do the same thing

66
00:02:22,360 --> 00:02:24,805
by concatenating strings using plus.

67
00:02:24,805 --> 00:02:26,320
But, this is a bit later.

68
00:02:26,320 --> 00:02:28,706
Alright and to avoid duplicating the Url,

69
00:02:28,706 --> 00:02:31,397
I'm going to change the oncreate code

70
00:02:31,397 --> 00:02:32,942
so that is uses these two fields

71
00:02:32,942 --> 00:02:36,215
to create the Url that's downloaded initially.

72
00:02:36,215 --> 00:02:38,937
So, to do that we're going to come down to oncreate method.

73
00:02:38,937 --> 00:02:41,852
We're going to change the download to euro method core.

74
00:02:41,852 --> 00:02:45,186
Going to delete out that hard code

75
00:02:45,186 --> 00:02:47,780
feed euro we have to replace that with

76
00:02:47,780 --> 00:02:48,613
feedeuro

77
00:02:50,429 --> 00:02:51,262
.format

78
00:02:53,482 --> 00:02:54,315
feedlimit

79
00:02:55,376 --> 00:02:58,073
(typing)

80
00:02:58,073 --> 00:02:59,755
So, that now uses the string dot format method

81
00:02:59,755 --> 00:03:02,539
to replace the percent d in the feed euro

82
00:03:02,539 --> 00:03:04,405
which we added on line 37

83
00:03:04,405 --> 00:03:07,490
with the current value of feed limit.

84
00:03:07,490 --> 00:03:08,683
The other thing that we're going to do

85
00:03:08,683 --> 00:03:11,683
is with our on options item created,

86
00:03:12,624 --> 00:03:13,957
we also want to fix that there as well.

87
00:03:13,957 --> 00:03:14,800
So, let's do the same thing.

88
00:03:14,800 --> 00:03:18,626
Now I'm going to take a copy of that,

89
00:03:18,626 --> 00:03:21,459
that line that download euro line.

90
00:03:23,026 --> 00:03:27,604
Come down here, we're going to replace it there.

91
00:03:27,604 --> 00:03:28,562
The other thing we need to do

92
00:03:28,562 --> 00:03:32,343
is we need to replace the percent d in three places

93
00:03:32,343 --> 00:03:33,858
in our on options item selected.

94
00:03:33,858 --> 00:03:35,638
So let's do that.

95
00:03:35,638 --> 00:03:36,471
Percent d.

96
00:03:38,351 --> 00:03:39,184
Percent d

97
00:03:40,985 --> 00:03:42,152
and percent d.

98
00:03:44,491 --> 00:03:45,666
Alright, so that's done.

99
00:03:45,666 --> 00:03:47,334
And, all we have to do now

100
00:03:47,334 --> 00:03:49,608
is change the value of feed limit

101
00:03:49,608 --> 00:03:52,052
when one of the two menu options is chosen.

102
00:03:52,052 --> 00:03:55,069
So, to do that we need another brant in our when expression.

103
00:03:55,069 --> 00:03:56,416
So, let's go ahead and do that.

104
00:03:56,416 --> 00:03:58,965
We're going to put this before the else.

105
00:03:58,965 --> 00:04:00,929
So, down here.

106
00:04:00,929 --> 00:04:02,012
And what we're going to type

107
00:04:02,012 --> 00:04:05,095
is r.id.mnu10

108
00:04:06,712 --> 00:04:11,688
,r.id.mnu25

109
00:04:11,688 --> 00:04:14,880
arrowtoken right blockade block

110
00:04:14,880 --> 00:04:16,789
and then we're going to put our logic in here

111
00:04:16,789 --> 00:04:20,678
to define whether to select 25 or 10.

112
00:04:20,678 --> 00:04:24,679
We'll do that by putting if not item.ischecked

113
00:04:25,602 --> 00:04:28,269
not is checkable but is checked.

114
00:04:29,409 --> 00:04:32,917
Then we put item.ischecked=true

115
00:04:32,917 --> 00:04:33,750
so if it's not checked

116
00:04:33,750 --> 00:04:35,158
we're setting it equal to checked.

117
00:04:35,158 --> 00:04:37,762
In other words we're setting it to true to do that.

118
00:04:37,762 --> 00:04:38,739
And we're going to put

119
00:04:38,739 --> 00:04:43,572
feedlimit=35takefeedlimit.

120
00:04:45,236 --> 00:04:46,069
Then, we're going to do a log

121
00:04:46,069 --> 00:04:51,069
so a log.d(tag,"onOptions

122
00:04:52,014 --> 00:04:54,097
(typing)

123
00:04:56,743 --> 00:05:01,743
itemselected, we'll put a colon there space

124
00:05:02,589 --> 00:05:04,498
a dollar sign left to right curly brackets

125
00:05:04,498 --> 00:05:09,498
item.title left side of the curly brackets setting

126
00:05:09,734 --> 00:05:14,151
feed limit to dollar feed limit.

127
00:05:15,107 --> 00:05:17,524
Then we'll put an else there.

128
00:05:20,218 --> 00:05:23,487
Let's just copy that line to save a bit of time.

129
00:05:23,487 --> 00:05:25,420
(typing)

130
00:05:25,420 --> 00:05:30,241
And this time, will be onOptionsItemSelectedItem feed limit

131
00:05:30,241 --> 00:05:32,282
and because it's an else, there wasn't any change

132
00:05:32,282 --> 00:05:36,461
so we're just going to put the text unchanged.

133
00:05:36,461 --> 00:05:37,701
So, we want them to perform pretty much

134
00:05:37,701 --> 00:05:41,379
the same actions whichever of the two options is selected.

135
00:05:41,379 --> 00:05:42,537
The only difference here is that

136
00:05:42,537 --> 00:05:45,419
the new value assigned to feed limit.

137
00:05:45,419 --> 00:05:48,197
And, you can see here that there's a neat little way

138
00:05:48,197 --> 00:05:51,598
to toggle between two different numerical values

139
00:05:51,598 --> 00:05:53,832
and that's just to subtract the current value

140
00:05:53,832 --> 00:05:54,931
from the sum of the two.

141
00:05:54,931 --> 00:05:58,728
So, in other words, 35 minus 25 is 10

142
00:05:58,728 --> 00:06:02,110
and 35 minus 10 is 25.

143
00:06:02,110 --> 00:06:04,032
So, that's how the limit changed here.

144
00:06:04,032 --> 00:06:05,648
Now, if an item was already checked

145
00:06:05,648 --> 00:06:07,123
then we don't need to do anything.

146
00:06:07,123 --> 00:06:10,007
Let's just test here on line 72.

147
00:06:10,007 --> 00:06:11,882
But, actually what we're doing we're making sure

148
00:06:11,882 --> 00:06:13,187
that it's not checked.

149
00:06:13,187 --> 00:06:15,066
So, if it's already checked we're going to ignore that

150
00:06:15,066 --> 00:06:17,846
and then the else code will be handled.

151
00:06:17,846 --> 00:06:20,739
And then we'll actually go through and just log some output

152
00:06:20,739 --> 00:06:23,390
to say that this setting of feed limit was unchanged.

153
00:06:23,390 --> 00:06:27,511
Otherwise, if the radiact button wasn't already selected

154
00:06:27,511 --> 00:06:28,867
or checked in other words,

155
00:06:28,867 --> 00:06:31,304
we're actually checking it and then we're updating

156
00:06:31,304 --> 00:06:34,353
feed limit then we're logging that output as well.

157
00:06:34,353 --> 00:06:36,235
Now, the way these radiact groups work

158
00:06:36,235 --> 00:06:39,337
isn't immediately obvious but it's actually quite clever.

159
00:06:39,337 --> 00:06:41,574
The menu items aren't checked or unchecked

160
00:06:41,574 --> 00:06:43,557
by the menu system when you click them.

161
00:06:43,557 --> 00:06:45,929
You have to do that yourself in code.

162
00:06:45,929 --> 00:06:49,086
So, if either one of those options is selected from the menu

163
00:06:49,086 --> 00:06:51,940
then we test the item to see if it was already selected.

164
00:06:51,940 --> 00:06:54,445
If it was, we don't need to do anything else

165
00:06:54,445 --> 00:06:56,638
or I'll have logged the fact that you can see

166
00:06:56,638 --> 00:06:58,641
with the else on line 77,

167
00:06:58,641 --> 00:07:01,255
so that we can see what's happening by checking the log cat.

168
00:07:01,255 --> 00:07:03,006
But, if the item wasn't checked,

169
00:07:03,006 --> 00:07:04,744
then we're setting it to checked.

170
00:07:04,744 --> 00:07:06,539
Now, the menu system does help here,

171
00:07:06,539 --> 00:07:09,141
it will automatically uncheck any other members

172
00:07:09,141 --> 00:07:11,009
of the same group for us.

173
00:07:11,009 --> 00:07:12,979
And that's handy because we only going to reference

174
00:07:12,979 --> 00:07:15,607
the one item passed into the method.

175
00:07:15,607 --> 00:07:17,469
And incidentally, that's why android passes

176
00:07:17,469 --> 00:07:21,496
in the item reference rather than just it's id.

177
00:07:21,496 --> 00:07:23,930
So, we have an item to manipulate when we need to

178
00:07:23,930 --> 00:07:25,526
like we're doing here.

179
00:07:25,526 --> 00:07:27,289
Now, if you want to you can find out more information

180
00:07:27,289 --> 00:07:31,378
about menu option groups in the documentation.

181
00:07:31,378 --> 00:07:34,467
Let me just bring that up on the screen.

182
00:07:34,467 --> 00:07:36,550
(typing)

183
00:07:39,353 --> 00:07:41,518
Now, this guide on screen actually covers

184
00:07:41,518 --> 00:07:43,819
quite a lot about using menus in general.

185
00:07:43,819 --> 00:07:45,930
The bit that's specific about groups

186
00:07:45,930 --> 00:07:47,683
is pretty well towards the bottom.

187
00:07:47,683 --> 00:07:49,658
About 4/5 of the way down.

188
00:07:49,658 --> 00:07:51,651
There's a section that should be there called

189
00:07:51,651 --> 00:07:55,626
something along the lines of using checkable menu items.

190
00:07:55,626 --> 00:07:58,573
(silence)

191
00:07:58,573 --> 00:08:01,222
See the card Using checkable menu items.

192
00:08:01,222 --> 00:08:02,085
Alright, so check that out

193
00:08:02,085 --> 00:08:04,257
if you want to find out more information.

194
00:08:04,257 --> 00:08:06,730
Alright, so let's go back to our app.

195
00:08:06,730 --> 00:08:08,447
So, essentially the app should toggle between

196
00:08:08,447 --> 00:08:11,004
showing the top 10 and top 25

197
00:08:11,004 --> 00:08:12,693
now that we've introduced as code.

198
00:08:12,693 --> 00:08:16,246
So, let's actually run it and see what happens.

199
00:08:16,246 --> 00:08:18,496
(clicking)

200
00:08:21,488 --> 00:08:24,519
Alright, so it starts off showing the top 10

201
00:08:24,519 --> 00:08:26,543
and what we can do is, we'll just scroll down

202
00:08:26,543 --> 00:08:30,000
and just confirm that it is the top 10.

203
00:08:30,000 --> 00:08:32,697
We're getting google maps to be the last one there.

204
00:08:32,697 --> 00:08:35,447
If you change this now to top 25.

205
00:08:37,039 --> 00:08:41,227
And have another look while scrolling down.

206
00:08:41,227 --> 00:08:42,907
Clearly, there's more applications showing now

207
00:08:42,907 --> 00:08:46,022
so that's good that the top 25 is now running as well.

208
00:08:46,022 --> 00:08:47,772
And we can also if we want to confirm that,

209
00:08:47,772 --> 00:08:49,691
have a look at log cat.

210
00:08:49,691 --> 00:08:53,608
And confirm that the change did actually occur.

211
00:08:55,390 --> 00:08:58,379
We can see the data being downloaded there.

212
00:08:58,379 --> 00:08:59,797
Just scroll up above that.

213
00:08:59,797 --> 00:09:03,047
Top 25 setting feed limit to 25.

214
00:09:04,005 --> 00:09:06,645
Let's make it easy, we can filter our main activity.

215
00:09:06,645 --> 00:09:07,478
Let's do that.

216
00:09:07,478 --> 00:09:08,645
Main activity.

217
00:09:09,847 --> 00:09:12,359
So, you can see now onOptionsItemSelected Top 25

218
00:09:12,359 --> 00:09:16,043
was selected and feed limit was set to 25.

219
00:09:16,043 --> 00:09:17,066
And just to confirm that,

220
00:09:17,066 --> 00:09:20,566
if you go back and set it to top 25 again.

221
00:09:22,503 --> 00:09:25,224
You can see this time, Top 25 setting feed limit unchanged.

222
00:09:25,224 --> 00:09:28,095
So, it's logging the fact that the limit wasn't changed.

223
00:09:28,095 --> 00:09:30,071
But, there is a problem though.

224
00:09:30,071 --> 00:09:32,248
And that's that it downloads the data again

225
00:09:32,248 --> 00:09:34,258
even though nothing's changed.

226
00:09:34,258 --> 00:09:35,571
And we can see that here,

227
00:09:35,571 --> 00:09:38,503
the fact that it's started the asynctask.

228
00:09:38,503 --> 00:09:41,599
Now, that's a good way to drain the device battery

229
00:09:41,599 --> 00:09:44,014
or incur roaming data charges.

230
00:09:44,014 --> 00:09:46,523
So, we really should do something about that.

231
00:09:46,523 --> 00:09:49,403
Now, by thinking back to the activity life cycle,

232
00:09:49,403 --> 00:09:52,254
you should be able to work out what will happen

233
00:09:52,254 --> 00:09:54,519
when I rotate the device into landscape.

234
00:09:54,519 --> 00:09:57,957
What I'm going to do is just clear the log cat

235
00:09:57,957 --> 00:09:59,394
so that we can see the log entry

236
00:09:59,394 --> 00:10:02,655
so download data will pass applications.

237
00:10:02,655 --> 00:10:05,522
Going to remove the filter to main activity.

238
00:10:05,522 --> 00:10:06,355
Actually I'll clear the log cat again

239
00:10:06,355 --> 00:10:07,526
now that I've done that.

240
00:10:07,526 --> 00:10:09,561
Now let's go and open up the app

241
00:10:09,561 --> 00:10:12,938
or bring that back onto the screen and let's rotate that

242
00:10:12,938 --> 00:10:14,688
and see what happens.

243
00:10:16,445 --> 00:10:18,974
So, when I do that you can see from the log cat here

244
00:10:18,974 --> 00:10:20,952
that it downloads the data again.

245
00:10:20,952 --> 00:10:25,785
And that's because the activity's destroyed and recreated.

246
00:10:27,036 --> 00:10:30,541
But, also if we come over here and check out the code,

247
00:10:30,541 --> 00:10:32,295
you can see what's happened here

248
00:10:32,295 --> 00:10:35,295
is that it set the limit back to 10,

249
00:10:36,908 --> 00:10:38,710
rather than 25.

250
00:10:38,710 --> 00:10:40,008
And that's again because the activities

251
00:10:40,008 --> 00:10:41,968
got destroyed and recreated.

252
00:10:41,968 --> 00:10:45,183
But, we know how to fix that by storing the value in these

253
00:10:45,183 --> 00:10:49,122
to state bundle is in the instant state function.

254
00:10:49,122 --> 00:10:53,736
Now, ideally the code should store the last used Url

255
00:10:53,736 --> 00:10:55,120
and only download the data again

256
00:10:55,120 --> 00:10:57,377
if the Url is actually changed.

257
00:10:57,377 --> 00:10:59,763
If we do that, we need to provide some way

258
00:10:59,763 --> 00:11:02,406
for the user to refresh the data manually.

259
00:11:02,406 --> 00:11:03,944
But, at least then, they'd be able to make

260
00:11:03,944 --> 00:11:06,370
a conscious decision to use their mobile data

261
00:11:06,370 --> 00:11:08,228
or use up some of the battery life

262
00:11:08,228 --> 00:11:10,842
rather than our app just doing it anyway.

263
00:11:10,842 --> 00:11:13,234
Unfortunately though, saving something as complex

264
00:11:13,234 --> 00:11:16,150
as an adapter, isn't as straight forward as just storing it

265
00:11:16,150 --> 00:11:18,183
in the instant state bundle.

266
00:11:18,183 --> 00:11:20,778
And that probably wouldn't be the best approach anyway.

267
00:11:20,778 --> 00:11:22,793
If we wanted to save performing a download

268
00:11:22,793 --> 00:11:24,938
with a device orientation changed.

269
00:11:24,938 --> 00:11:27,374
It's probably better to cache the downloaded data

270
00:11:27,374 --> 00:11:30,403
in the same way that web browsers do.

271
00:11:30,403 --> 00:11:32,224
So, that way you can allow your activity

272
00:11:32,224 --> 00:11:36,027
to be destroyed and recreated when the device is rotated

273
00:11:36,027 --> 00:11:38,147
and the redownloading would read the data

274
00:11:38,147 --> 00:11:41,505
from the local cache instead of over the internet.

275
00:11:41,505 --> 00:11:44,221
Now, that's a bit much for only the 3rd app in the course.

276
00:11:44,221 --> 00:11:46,645
So, this app's just going to download the data again

277
00:11:46,645 --> 00:11:48,639
when the device is rotated.

278
00:11:48,639 --> 00:11:51,404
But, if you do want to learn more about caching downloads,

279
00:11:51,404 --> 00:11:53,837
there's a pretty decent Google article,

280
00:11:53,837 --> 00:11:57,438
which I'm just going to bring up on the screen to check out.

281
00:11:57,438 --> 00:11:59,688
(clicking)

282
00:12:00,780 --> 00:12:03,053
And basically, we can prevent redundant downloads

283
00:12:03,053 --> 00:12:05,687
when menu items are clicked though

284
00:12:05,687 --> 00:12:08,043
and can also make sure that the correct Url

285
00:12:08,043 --> 00:12:11,820
and feed limit are used when the device is rotated.

286
00:12:11,820 --> 00:12:14,519
So, that sounds like a good challenge.

287
00:12:14,519 --> 00:12:16,985
Now, one problem you'll have that I haven't covered

288
00:12:16,985 --> 00:12:19,476
is how to set the current menu limit

289
00:12:19,476 --> 00:12:22,076
once we've restored the feed limit value.

290
00:12:22,076 --> 00:12:23,663
Now, the place to do that

291
00:12:23,663 --> 00:12:26,508
is in the OnCreateOptionsMenu method.

292
00:12:26,508 --> 00:12:27,812
Now, the approach is very similar

293
00:12:27,812 --> 00:12:29,966
to working with widgets in a layout.

294
00:12:29,966 --> 00:12:32,960
But, here we're working with items in a menu.

295
00:12:32,960 --> 00:12:36,565
So, let's actually have a look at that.

296
00:12:36,565 --> 00:12:40,612
So, OnCreateOptionsMenu, you've got the menu.flatline

297
00:12:40,612 --> 00:12:42,806
and what we want to do after that

298
00:12:42,806 --> 00:12:45,293
is put in some code that says if

299
00:12:45,293 --> 00:12:47,376
feed limit is equal to 10

300
00:12:50,531 --> 00:12:54,114
then we're going to do a menu question mark

301
00:12:55,860 --> 00:12:56,860
find item

302
00:12:58,157 --> 00:13:03,157
R.id.mnu10 question mark outside of the parenthesis

303
00:13:03,896 --> 00:13:06,979
.ischecked is equal to true,

304
00:13:07,942 --> 00:13:09,446
we're going to add an else otherwise.

305
00:13:09,446 --> 00:13:12,613
And the else is going to be basically very similar

306
00:13:12,613 --> 00:13:15,038
but it's going to be the r and the mnu25,

307
00:13:15,038 --> 00:13:17,511
so the top 25 button.

308
00:13:17,511 --> 00:13:18,344
So, your else will be

309
00:13:18,344 --> 00:13:23,344
menu question mark.find item parenthesis r.id.mnu25

310
00:13:24,538 --> 00:13:26,153
then a question mark outside of the parenthesis

311
00:13:26,153 --> 00:13:28,070
.ischecked equals true.

312
00:13:29,444 --> 00:13:32,228
And then we just kind of return to what it was before.

313
00:13:32,228 --> 00:13:34,038
So, we used the menu.finditem method

314
00:13:34,038 --> 00:13:36,486
then set the ischecked property to check

315
00:13:36,486 --> 00:13:39,674
either the mnu10 or mnu25 item

316
00:13:39,674 --> 00:13:42,125
depending on the value of feed limit.

317
00:13:42,125 --> 00:13:44,129
Now, notice that we're using the safe call operator

318
00:13:44,129 --> 00:13:45,491
a lot here.

319
00:13:45,491 --> 00:13:49,439
Menu is a nullable type, so we have to use safe call

320
00:13:49,439 --> 00:13:51,336
when calling it's finditem method.

321
00:13:51,336 --> 00:13:55,087
And we also use it before accessing the ischecked property.

322
00:13:55,087 --> 00:13:56,671
Now, you might be wondering why I didn't just change

323
00:13:56,671 --> 00:14:00,165
the function signature, after all I've been doing so

324
00:14:00,165 --> 00:14:02,623
with apparent appeal up until now.

325
00:14:02,623 --> 00:14:05,379
Well, as I said, you need to think very carefully

326
00:14:05,379 --> 00:14:06,932
before doing that.

327
00:14:06,932 --> 00:14:09,673
Now in here, we can't be certain that we can do it.

328
00:14:09,673 --> 00:14:11,766
So, our app could use one of the no action bar themes

329
00:14:11,766 --> 00:14:13,582
which means it wouldn't have a menu.

330
00:14:13,582 --> 00:14:16,140
So, in that case menu would be null.

331
00:14:16,140 --> 00:14:18,399
Now, it's likely that OnCreateOptionsMenu

332
00:14:18,399 --> 00:14:20,397
won't be called in that case.

333
00:14:20,397 --> 00:14:22,638
You'd have to examine the android framework code

334
00:14:22,638 --> 00:14:24,857
to be sure which would take some time.

335
00:14:24,857 --> 00:14:27,960
So, it's easier just to use safe calls,

336
00:14:27,960 --> 00:14:29,557
at least in this scenario.

337
00:14:29,557 --> 00:14:33,256
If in doubt, don't modify the framework's signatures.

338
00:14:33,256 --> 00:14:37,121
Now, of course if Google added a non-null annotation

339
00:14:37,121 --> 00:14:39,607
then you'll know that we could've changed it.

340
00:14:39,607 --> 00:14:41,898
When that happens the code generator will generate

341
00:14:41,898 --> 00:14:44,400
the method without using a nullable type.

342
00:14:44,400 --> 00:14:46,991
Alright, so with that change, you should now be able

343
00:14:46,991 --> 00:14:48,487
to have a go at the challenge.

344
00:14:48,487 --> 00:14:50,916
So, here's the challenge.

345
00:14:50,916 --> 00:14:51,977
So, you want to modify the apps,

346
00:14:51,977 --> 00:14:56,455
so it doesn't download data from the same Url a second time

347
00:14:56,455 --> 00:15:00,048
unless the user specifically chooses to refresh the data.

348
00:15:00,048 --> 00:15:02,750
Now, you also store the current Url and limit

349
00:15:02,750 --> 00:15:04,219
so that they aren't reset

350
00:15:04,219 --> 00:15:06,548
when the device orientation changes,

351
00:15:06,548 --> 00:15:08,175
the data will still be downloaded again

352
00:15:08,175 --> 00:15:09,954
to follow the change in orientation.

353
00:15:09,954 --> 00:15:12,505
But, it should download from the same Url

354
00:15:12,505 --> 00:15:16,463
that the user was looking at before they rotated the device.

355
00:15:16,463 --> 00:15:18,185
So, to be clear, the only three events

356
00:15:18,185 --> 00:15:20,887
that should cause data to be downloaded

357
00:15:20,887 --> 00:15:23,650
are firstly, a change in orientation.

358
00:15:23,650 --> 00:15:27,677
Secondly, selecting a different Url or limit from the menu.

359
00:15:27,677 --> 00:15:29,856
So, if the same menu Url or limit is selected from

360
00:15:29,856 --> 00:15:32,881
the menu a second time, the data wouldn't be downloaded.

361
00:15:32,881 --> 00:15:36,157
Thirdly, if the user chooses to refresh the feed,

362
00:15:36,157 --> 00:15:37,407
with the refresh menu option

363
00:15:37,407 --> 00:15:39,721
that you're going to actually add.

364
00:15:39,721 --> 00:15:40,696
And just as a hint there,

365
00:15:40,696 --> 00:15:43,344
review the Activity Lifecycle before restoring

366
00:15:43,344 --> 00:15:45,212
the data from the bundle.

367
00:15:45,212 --> 00:15:47,961
Now, there's more than one way to achieve this challenge.

368
00:15:47,961 --> 00:15:50,675
So, the real test here is just that the app works.

369
00:15:50,675 --> 00:15:52,468
Alright, so I'll go over my solution to this

370
00:15:52,468 --> 00:15:54,051
in the next video.

