1 00:00:04,750 --> 00:00:07,240 Okay we're now ready to run the app and 2 00:00:07,240 --> 00:00:09,700 investigate the various stages of the 3 00:00:09,700 --> 00:00:12,369 activity lifecycle. Now before I do that, 4 00:00:12,369 --> 00:00:13,600 though, I wanted to talk briefly about 5 00:00:13,600 --> 00:00:15,189 the panes down here, these various 6 00:00:15,189 --> 00:00:17,619 windows that you can pop up, and that's 7 00:00:17,619 --> 00:00:19,300 because you can actually reposition them, 8 00:00:19,300 --> 00:00:21,070 using the cog wheel over here on the 9 00:00:21,070 --> 00:00:22,900 right hand side. So I'm going to come 10 00:00:22,900 --> 00:00:24,789 over here and choose Floating mode from 11 00:00:24,789 --> 00:00:27,189 the pop-up menu. Once we do that, you can 12 00:00:27,189 --> 00:00:29,439 see we've got just a little window that 13 00:00:29,439 --> 00:00:31,779 is now separate from the main Android 14 00:00:31,779 --> 00:00:33,610 Studio window, and we can move that 15 00:00:33,610 --> 00:00:36,070 around. So that can be really handy if 16 00:00:36,070 --> 00:00:37,810 you've got a single monitor, because you 17 00:00:37,810 --> 00:00:39,400 can display that and the emulator at the 18 00:00:39,400 --> 00:00:41,320 same time, or we can actually see a lot 19 00:00:41,320 --> 00:00:43,210 more screen real estate which we're 20 00:00:43,210 --> 00:00:44,950 going to be doing here in this video. But 21 00:00:44,950 --> 00:00:46,960 it's also useful if you've got two 22 00:00:46,960 --> 00:00:48,730 monitors, because you can put logcat 23 00:00:48,730 --> 00:00:50,680 on one window and the main programming 24 00:00:50,680 --> 00:00:52,960 code, your designer and so forth, and see 25 00:00:52,960 --> 00:00:54,820 the code on the other monitor, so quite 26 00:00:54,820 --> 00:00:57,280 handy. Alright, so let's run this app. So 27 00:00:57,280 --> 00:00:58,720 I'm going to leave this logcat open 28 00:00:58,720 --> 00:01:01,750 here, run our app. I'll just need to click 29 00:01:01,750 --> 00:01:02,770 that to bring that back on the screen 30 00:01:02,770 --> 00:01:05,110 again, and I'll also bring the emulator 31 00:01:05,110 --> 00:01:07,840 over. So there's our app, and you can see 32 00:01:07,840 --> 00:01:11,020 over here now in logcat, that the 33 00:01:11,020 --> 00:01:12,940 onCreate method's been called, up here, 34 00:01:12,940 --> 00:01:15,370 then the on Start and then the onResume. 35 00:01:15,370 --> 00:01:17,680 And that's what we would've expected 36 00:01:17,680 --> 00:01:19,690 from the activity lifecycle. Now 37 00:01:19,690 --> 00:01:21,280 because this was the first time the app 38 00:01:21,280 --> 00:01:22,930 was launched, there's no state to be 39 00:01:22,930 --> 00:01:25,600 restored, so consequently onRestore 40 00:01:25,600 --> 00:01:28,479 InstanceState wasn't called. Alright, 41 00:01:28,479 --> 00:01:30,250 so let's add some text now and click the 42 00:01:30,250 --> 00:01:31,810 button a few times and keep an eye on 43 00:01:31,810 --> 00:01:34,690 the logcat, because what 44 00:01:34,690 --> 00:01:36,070 we're going to do is try rotating it 45 00:01:36,070 --> 00:01:38,170 into landscape mode. But first, I'm going 46 00:01:38,170 --> 00:01:42,370 to enter these three items; bread - and you 47 00:01:42,370 --> 00:01:43,659 can see onClick has been called in 48 00:01:43,659 --> 00:01:48,970 the logcat there - milk, cheese. So we've 49 00:01:48,970 --> 00:01:50,799 got three items there, and what I'm going 50 00:01:50,799 --> 00:01:52,690 to do is make sure there's some text in 51 00:01:52,690 --> 00:01:54,040 the editText and then we're going to move 52 00:01:54,040 --> 00:01:56,590 it into landscape mode. So I'm going to 53 00:01:56,590 --> 00:02:01,119 type in cream here, then move over to 54 00:02:01,119 --> 00:02:04,390 landscape. Then you can see we've still got 55 00:02:04,390 --> 00:02:06,159 cream showing there, and we can see that 56 00:02:06,159 --> 00:02:08,530 over here now, the various events that 57 00:02:08,530 --> 00:02:09,908 were called. We had our onClick method 58 00:02:09,908 --> 00:02:11,049 called three times, which you would have 59 00:02:11,049 --> 00:02:12,700 expected for the three items because I 60 00:02:12,700 --> 00:02:14,319 didn't click the button the fourth time. 61 00:02:14,319 --> 00:02:16,959 But then what happened was, when the 62 00:02:16,959 --> 00:02:18,530 device was rotated, 63 00:02:18,530 --> 00:02:20,750 the onPause method was called, followed 64 00:02:20,750 --> 00:02:23,090 by onSaveInstanceState, and then on 65 00:02:23,090 --> 00:02:26,390 Stop and then onDestroy. And you may 66 00:02:26,390 --> 00:02:28,760 have seen that the app briefly vanished 67 00:02:28,760 --> 00:02:30,650 from the device screen, briefly, while it 68 00:02:30,650 --> 00:02:33,890 was rotating into landscape mode. Now 69 00:02:33,890 --> 00:02:35,810 because it was Android destroying the 70 00:02:35,810 --> 00:02:37,489 activity, and it was in the foreground, 71 00:02:37,489 --> 00:02:39,980 Android then recreates the activity 72 00:02:39,980 --> 00:02:42,290 again. So that's where the onCreate, on 73 00:02:42,290 --> 00:02:45,709 Start is actually called. And because 74 00:02:45,709 --> 00:02:47,600 we've got now some state because on 75 00:02:47,600 --> 00:02:49,670 SaveInstanceState was called prior to 76 00:02:49,670 --> 00:02:52,190 going into landscape mode, as a result, on 77 00:02:52,190 --> 00:02:54,260 RestoreInstanceState is called automatically 78 00:02:54,260 --> 00:02:56,450 by Android. And then finally, 79 00:02:56,450 --> 00:02:58,310 we've got on the onResume method being 80 00:02:58,310 --> 00:03:00,530 called as well. And after that the app's 81 00:03:00,530 --> 00:03:02,000 once again running in the foreground, 82 00:03:02,000 --> 00:03:04,489 which it's doing there now. So let's add a 83 00:03:04,489 --> 00:03:05,930 comment down here, just so we know where we're 84 00:03:05,930 --> 00:03:09,739 at. We'll type in Back Button, because what 85 00:03:09,739 --> 00:03:12,080 we're about to do is now select the back 86 00:03:12,080 --> 00:03:14,150 button to move back. Firstly, I'm going to 87 00:03:14,150 --> 00:03:16,670 remove the keyboard by pressing back, so 88 00:03:16,670 --> 00:03:17,810 we're not expecting anything to be 89 00:03:17,810 --> 00:03:19,580 called there. But now if I actually go 90 00:03:19,580 --> 00:03:20,540 back again, 91 00:03:20,540 --> 00:03:23,180 it's gonna close the app. And what's 92 00:03:23,180 --> 00:03:24,769 happened here, you can see that when I've 93 00:03:24,769 --> 00:03:26,060 actually done that, we've got onPause 94 00:03:26,060 --> 00:03:29,150 called, onStop called and then on 95 00:03:29,150 --> 00:03:30,079 Destroy called, 96 00:03:30,079 --> 00:03:32,480 and obviously at that point the app was 97 00:03:32,480 --> 00:03:34,280 then closed. Note that there wasn't a 98 00:03:34,280 --> 00:03:36,500 call this time, to onSaveInstanceState, 99 00:03:36,500 --> 00:03:38,450 and that's because the state won't be 100 00:03:38,450 --> 00:03:40,430 restored when the app's relaunched. 101 00:03:40,430 --> 00:03:42,890 Because Android only restores state if 102 00:03:42,890 --> 00:03:45,470 it was responsible for destroying the 103 00:03:45,470 --> 00:03:47,810 activity. Now if the user chooses to 104 00:03:47,810 --> 00:03:49,549 close the app, then they wouldn't expect 105 00:03:49,549 --> 00:03:51,590 it to start up with the data already in 106 00:03:51,590 --> 00:03:54,650 the editText from the last run. With 107 00:03:54,650 --> 00:03:55,730 that said though, they may however 108 00:03:55,730 --> 00:03:57,920 expect to see the bread, milk and cheese 109 00:03:57,920 --> 00:04:00,140 in the TextView, but if you wanted to 110 00:04:00,140 --> 00:04:02,450 save those results, then onStop would be 111 00:04:02,450 --> 00:04:04,760 the place to do that. Now because the 112 00:04:04,760 --> 00:04:05,299 logcat 113 00:04:05,299 --> 00:04:07,430 gets cleared when we run an app from 114 00:04:07,430 --> 00:04:09,410 Android Studio, what I'm going to do is 115 00:04:09,410 --> 00:04:11,810 use the emulator's app screen to launch 116 00:04:11,810 --> 00:04:14,900 our button click counter app again. So 117 00:04:14,900 --> 00:04:15,950 I'm gonna type in a comment though, into 118 00:04:15,950 --> 00:04:20,390 logcat first, and just put launching app, and 119 00:04:20,390 --> 00:04:22,669 then I'm going to come back here and 120 00:04:22,669 --> 00:04:23,840 we're going to launch the app. I'm going to 121 00:04:23,840 --> 00:04:24,950 do that sideways because I'm still in 122 00:04:24,950 --> 00:04:27,289 landscape mode, buttoncounterapp, I'm 123 00:04:27,289 --> 00:04:29,630 gonna click on that. There's our button 124 00:04:29,630 --> 00:04:31,790 counter app running again now, 125 00:04:31,790 --> 00:04:33,980 and looking at the log, you can see that 126 00:04:33,980 --> 00:04:35,600 we've got onCreate, onStart and on 127 00:04:35,600 --> 00:04:37,790 Resume called, so that's good. Then what 128 00:04:37,790 --> 00:04:40,240 we'll do now is, we'll just go back to 129 00:04:40,240 --> 00:04:42,970 portrait mode. 130 00:04:42,970 --> 00:04:44,900 There's portrait mode again there now, 131 00:04:44,900 --> 00:04:45,890 just so it's a little bit easier. We can 132 00:04:45,890 --> 00:04:48,770 see a bit more of the log and 133 00:04:48,770 --> 00:04:50,210 the application running on our 134 00:04:50,210 --> 00:04:52,490 emulator as well. And we can see now, 135 00:04:52,490 --> 00:04:54,080 that by doing that this second time, 136 00:04:54,080 --> 00:04:56,210 we've got onCreate called because I 137 00:04:56,210 --> 00:04:58,520 went back to portrait mode onStart. And 138 00:04:58,520 --> 00:05:00,560 onSaveInstanceState, by the way, was 139 00:05:00,560 --> 00:05:03,340 called just prior to, or just after we 140 00:05:03,340 --> 00:05:05,900 rotated the device back to portrait. So 141 00:05:05,900 --> 00:05:07,130 while it was still in that 142 00:05:07,130 --> 00:05:09,080 process of moving back to portrait, on 143 00:05:09,080 --> 00:05:10,670 SaveInstanceState was called. Then 144 00:05:10,670 --> 00:05:12,860 consequently down here, onStart, on 145 00:05:12,860 --> 00:05:14,510 RestoreInstanceState was called then 146 00:05:14,510 --> 00:05:16,070 onResume, and then we're back with 147 00:05:16,070 --> 00:05:17,780 the app running in portrait mode again. 148 00:05:17,780 --> 00:05:20,300 Alright, you can sort of see there, the lifecycle 149 00:05:20,300 --> 00:05:22,340 is being fairly consistent there. We'll type 150 00:05:22,340 --> 00:05:24,350 in Locking Screen next, so I'm gonna type 151 00:05:24,350 --> 00:05:28,520 Locking Screen into logcat, and what 152 00:05:28,520 --> 00:05:29,960 we're gonna do now is just that. We're going 153 00:05:29,960 --> 00:05:32,330 to briefly lock the screen, and on an 154 00:05:32,330 --> 00:05:34,280 emulator, we can do that by the power 155 00:05:34,280 --> 00:05:36,200 button. So if we just press the power 156 00:05:36,200 --> 00:05:38,510 button quickly, and just then release it 157 00:05:38,510 --> 00:05:40,250 fairly quickly, then that locks it, just 158 00:05:40,250 --> 00:05:42,020 like a real Android device would. 159 00:05:42,020 --> 00:05:43,820 If we hold it down it'll shut it 160 00:05:43,820 --> 00:05:45,260 down. So I'm just going to click it 161 00:05:45,260 --> 00:05:48,740 briefly, and there's our lock, so 162 00:05:48,740 --> 00:05:50,180 the application is now going to lock, 163 00:05:50,180 --> 00:05:51,800 bearing in mind we've got some extra 164 00:05:51,800 --> 00:05:54,140 bits and pieces showing here as well 165 00:05:54,140 --> 00:05:56,270 which we're ignoring. But the ones that 166 00:05:56,270 --> 00:05:57,830 we want to see here is the onPause 167 00:05:57,830 --> 00:06:00,260 called, onSaveInstanceState called and 168 00:06:00,260 --> 00:06:03,560 then onstop called. So note that the 169 00:06:03,560 --> 00:06:06,350 user hasn't exited the app as such. They've 170 00:06:06,350 --> 00:06:07,730 just switched off the screen, and that's 171 00:06:07,730 --> 00:06:09,590 why Android's calling onSaveInstance 172 00:06:09,590 --> 00:06:12,830 State after onPause. And onStop's then 173 00:06:12,830 --> 00:06:14,390 called and our app's now no longer running 174 00:06:14,390 --> 00:06:16,610 in the foreground. So I'm gonna add 175 00:06:16,610 --> 00:06:19,580 another comment down here now, and we'll 176 00:06:19,580 --> 00:06:23,840 put Unlock Screen, and we're gonna use 177 00:06:23,840 --> 00:06:25,280 the power button to unlock the screen. So 178 00:06:25,280 --> 00:06:27,710 I'm gonna click it once quickly again. So 179 00:06:27,710 --> 00:06:29,480 just like a normal Android app, we now need 180 180 00:06:29,480 --> 00:06:31,100 to swipe to get back to where we were 181 00:06:31,100 --> 00:06:33,040 again. 182 00:06:33,040 --> 00:06:34,820 Alright, so at this point you might be 183 00:06:34,820 --> 00:06:36,470 wondering, the screen was locked, 184 00:06:36,470 --> 00:06:37,580 when the screen was locked, 185 00:06:37,580 --> 00:06:40,400 we had onSaveInstanceState called. So why 186 00:06:40,400 --> 00:06:42,080 wasn't onRestoreInstanceState called 187 00:06:42,080 --> 00:06:44,150 when we unlocked the screen? Well there's 188 00:06:44,150 --> 00:06:45,470 no reason to call it, and 189 00:06:45,470 --> 00:06:46,880 that's because the app, or the 190 00:06:46,880 --> 00:06:48,590 activity rather, wasn't destroyed, 191 00:06:48,590 --> 00:06:50,720 it was just stopped. So in other words, no 192 00:06:50,720 --> 00:06:52,760 state was lost and there's no reason to 193 00:06:52,760 --> 00:06:54,500 restore it. We can actually check that 194 00:06:54,500 --> 00:06:57,080 though. What we can do, is we can go ahead 195 00:06:57,080 --> 00:06:58,730 and enter our items again. So I can enter 196 00:06:58,730 --> 00:07:10,130 bread, milk, cheese, and we're going into 197 00:07:10,130 --> 00:07:11,330 cream again but this time we're not 198 00:07:11,330 --> 00:07:14,120 going to tap the button like we did last 199 00:07:14,120 --> 00:07:20,420 time. It'll lock the screen again, and you 200 00:07:20,420 --> 00:07:21,590 can see what's happened here. 201 00:07:21,590 --> 00:07:23,990 We've got, I think this is more Android 202 00:07:23,990 --> 00:07:26,120 Studio functionality problems 203 00:07:26,120 --> 00:07:28,220 here, because we've got the Unlock Screen 204 00:07:28,220 --> 00:07:30,590 and you saw that when I went back to it, 205 00:07:30,590 --> 00:07:33,020 nothing seemed to be working for a 206 00:07:33,020 --> 00:07:34,460 while. We weren't getting any entries into 207 00:07:34,460 --> 00:07:36,710 here, and then when I've locked the screen, 208 00:07:36,710 --> 00:07:37,880 we've suddenly got all these entries 209 00:07:37,880 --> 00:07:39,860 coming back through again. So the point I 210 00:07:39,860 --> 00:07:42,080 want to make here, though, is that after 211 00:07:42,080 --> 00:07:43,880 we unlocked the screen, we actually did 212 00:07:43,880 --> 00:07:46,670 get onRestart called and we got onStart 213 00:07:46,670 --> 00:07:49,400 called, and then we also got onResume 214 00:07:49,400 --> 00:07:50,900 called. All three of them were called very 215 00:07:50,900 --> 00:07:52,250 quickly, as you can see here by the time, 216 00:07:52,250 --> 00:07:55,010 over to the left-hand side. But the point 217 00:07:55,010 --> 00:07:57,020 was that onRestoreInstanceState wasn't 218 00:07:57,020 --> 00:07:59,060 called when we unlocked the screen. And 219 00:07:59,060 --> 00:08:00,950 again, the reason for that is that from 220 00:08:00,950 --> 00:08:02,780 Android Studio, there actually wasn't a 221 00:08:02,780 --> 00:08:04,430 reason to call it, and that's because 222 00:08:04,430 --> 00:08:06,140 the activity wasn't destroyed. 223 00:08:06,140 --> 00:08:08,930 It was just stopped, so no state was lost 224 00:08:08,930 --> 00:08:11,300 and there's no reason to restore it. You 225 00:08:11,300 --> 00:08:12,380 also saw, even though I've locked the 226 00:08:12,380 --> 00:08:15,050 screen now, we did get the onClick calls 227 00:08:15,050 --> 00:08:17,360 successfully being called when we 228 00:08:17,360 --> 00:08:18,320 clicked the buttons, as we would expect, 229 00:08:18,320 --> 00:08:20,300 but they weren't updated in the log. I 230 00:08:20,300 --> 00:08:22,070 suspect that's some sort of issue with 231 00:08:22,070 --> 00:08:23,930 logcat, probably because I'm running a 232 00:08:23,930 --> 00:08:26,000 beta version of Android Studio. You can 233 00:08:26,000 --> 00:08:27,470 see that they were actually called, and 234 00:08:27,470 --> 00:08:29,660 then when I locked the screen, we got on 235 00:08:29,660 --> 00:08:31,760 Pause called and then onSaveInstanceState 236 00:08:31,760 --> 00:08:34,700 called and then as well as onStop. So at 237 00:08:34,700 --> 00:08:36,080 this point, we've now locked the screen, 238 00:08:36,080 --> 00:08:38,299 and again onSaveInstanceState's called 239 00:08:38,299 --> 00:08:40,429 because Android might destroy the 240 00:08:40,429 --> 00:08:42,260 activity when the screen's locked. 241 00:08:42,260 --> 00:08:44,420 Now if updates start downloading or 242 00:08:44,420 --> 00:08:46,520 some other app wakes up, memory may get 243 00:08:46,520 --> 00:08:48,620 low. Because our app is no longer in the 244 00:08:48,620 --> 00:08:50,750 foreground, it's fair game for Android to 245 00:08:50,750 --> 00:08:52,370 destroy it if it needs extra resources. 246 00:08:52,370 --> 00:08:55,190 So that's the reason that onSave 247 00:08:55,190 --> 00:08:57,440 InstanceState is actually called here, when 248 00:08:57,440 --> 00:08:58,649 the screen's actually locked. 249 00:08:58,649 --> 00:09:01,290 Now with that said, though, unless Android 250 00:09:01,290 --> 00:09:03,180 does destroy the activity, then there's 251 00:09:03,180 --> 00:09:05,009 no need for it to restore the state. So 252 00:09:05,009 --> 00:09:06,509 that's way, in this case, it didn't call 253 00:09:06,509 --> 00:09:08,959 the onRestoreInstanceState when we 254 00:09:08,959 --> 00:09:11,069 unlocked the screen. So the same thing 255 00:09:11,069 --> 00:09:12,389 should happen now if I go to unlock the 256 00:09:12,389 --> 00:09:16,230 screen again. We've still got our items 257 00:09:16,230 --> 00:09:19,019 here so nothing's been destroyed, but our 258 00:09:19,019 --> 00:09:20,879 log is not working at the moment but, 259 00:09:20,879 --> 00:09:22,740 hopefully, that'll catch back up. 260 00:09:22,740 --> 00:09:24,839 But in any event, I can assure you like 261 00:09:24,839 --> 00:09:27,240 it happened last time, that the on 262 00:09:27,240 --> 00:09:29,939 RestoreInstanceState won't be called, and 263 00:09:29,939 --> 00:09:31,920 that's because the application was 264 00:09:31,920 --> 00:09:34,019 stopped and it wasn't destroyed. So it's 265 00:09:34,019 --> 00:09:35,399 just there as we left it, in other words. 266 00:09:35,399 --> 00:09:37,259 We can see that by the three entries 267 00:09:37,259 --> 00:09:38,879 that we've got here, and also the entry 268 00:09:38,879 --> 00:09:40,740 still showing in the editText. So 269 00:09:40,740 --> 00:09:42,929 again, if Android did destroy the 270 00:09:42,929 --> 00:09:45,119 activity, then we wouldn't expect to see 271 00:09:45,119 --> 00:09:47,639 our bread, milk and cheese here, because 272 00:09:47,639 --> 00:09:49,529 normally on the onRestoreInstance 273 00:09:49,529 --> 00:09:52,829 States is called, this isn't actually 274 00:09:52,829 --> 00:09:54,209 saved because we haven't written any 275 00:09:54,209 --> 00:09:55,499 code to do that yet. The only thing that 276 00:09:55,499 --> 00:09:58,439 is normally saved is this option, 277 00:09:58,439 --> 00:10:01,470 whatever we've typed in the editText. So 278 00:10:01,470 --> 00:10:03,179 again, unless Android does destroy the 279 00:10:03,179 --> 00:10:04,439 activity there's no need for it to 280 00:10:04,439 --> 00:10:06,480 restore the state, so that's why in this 281 00:10:06,480 --> 00:10:08,730 case, onRestoreInstanceState wasn't being 282 00:10:08,730 --> 00:10:10,649 called. It wasn't called last time and 283 00:10:10,649 --> 00:10:13,050 wasn't called this time either. But again, 284 00:10:13,050 --> 00:10:14,579 we can't really confirm that right now 285 00:10:14,579 --> 00:10:17,249 because Android Studio seems to be 286 00:10:17,249 --> 00:10:19,920 playing up a little bit, and it's not 287 00:10:19,920 --> 00:10:21,540 actually showing us all the logs. So I 288 00:10:21,540 --> 00:10:24,720 can see that it seems to just have been 289 00:10:24,720 --> 00:10:26,610 stalled there and it's not actually updating, 290 00:10:26,610 --> 00:10:27,870 and I can confirm that by trying to add 291 00:10:27,870 --> 00:10:30,899 a click on the button now. We've added 292 00:10:30,899 --> 00:10:32,459 that here but we're not getting any log 293 00:10:32,459 --> 00:10:34,259 intries in the logcat. So clearly a bug 294 00:10:34,259 --> 00:10:36,959 with Android Studio at the moment, but 295 00:10:36,959 --> 00:10:38,339 anyway, you've seen that work earlier in 296 00:10:38,339 --> 00:10:39,870 the video. So that's actually the 297 00:10:39,870 --> 00:10:42,480 activity lifecycle. So that technique, by 298 00:10:42,480 --> 00:10:43,949 the way, of adding logging to all the 299 00:10:43,949 --> 00:10:46,439 methods is great to see, or for seeing 300 00:10:46,439 --> 00:10:48,720 what's going on, and when various methods 301 00:10:48,720 --> 00:10:50,879 are called. Now it doesn't just apply to 302 00:10:50,879 --> 00:10:53,399 the lifecycle methods either. There's 303 00:10:53,399 --> 00:10:55,350 all sorts of situations where we have to 304 00:10:55,350 --> 00:10:57,230 provide callbacks for Android to call. 305 00:10:57,230 --> 00:10:59,220 Adding logging to them so that you can 306 00:10:59,220 --> 00:11:01,230 see exactly when Android does call them, 307 00:11:01,230 --> 00:11:03,029 is a very good way to develop your 308 00:11:03,029 --> 00:11:05,999 understanding of how Android works. Alright, 309 00:11:05,999 --> 00:11:07,829 so just to show you this before I finish 310 00:11:07,829 --> 00:11:09,779 this video, that the logcat should still 311 00:11:09,779 --> 00:11:11,819 be working. I believe it's a problem to 312 00:11:11,819 --> 00:11:12,420 do with 313 00:11:12,420 --> 00:11:14,520 Android Studio, probably linking to the 314 00:11:14,520 --> 00:11:15,750 emulator. So if I go to close this 315 00:11:15,750 --> 00:11:19,560 emulator down, hopefully, what we'll see 316 00:11:19,560 --> 00:11:21,990 is some errors pop up because I want to see one 317 00:11:21,990 --> 00:11:23,550 of those new log entries actually just 318 00:11:23,550 --> 00:11:25,200 come through. At the moment they haven't 319 00:11:25,200 --> 00:11:27,120 come back through, since we locked the 320 00:11:27,120 --> 00:11:29,040 screen for the second time. I'm gonna 321 00:11:29,040 --> 00:11:33,480 just try this again, start our API 26 322 00:11:33,480 --> 00:11:35,670 emulator. At some point, we'll probably 323 00:11:35,670 --> 00:11:44,530 see these entries. 324 00:11:44,530 --> 00:11:47,080 So we're still not getting any logging 325 00:11:47,080 --> 00:11:49,240 output there. So what I might do is I'm 326 00:11:49,240 --> 00:11:52,360 just gonna close down Android Studio, but 327 00:11:52,360 --> 00:11:54,840 at some point what we do here, 328 00:11:54,840 --> 00:11:58,210 we should find this gets fixed up. This 329 00:11:58,210 --> 00:12:00,400 will be a good thing to show you if you're 330 00:12:00,400 --> 00:12:01,900 in a similar situation where you're 331 00:12:01,900 --> 00:12:03,040 getting a lock up and you're not seeing 332 00:12:03,040 --> 00:12:05,470 logcat working. The combination of these 333 00:12:05,470 --> 00:12:07,620 things should actually fix the problem. 334 00:12:07,620 --> 00:12:09,700 Alright, so I'm starting that again. I'm 335 00:12:09,700 --> 00:12:12,310 gonna go and open logcat, and as we would 336 00:12:12,310 --> 00:12:15,130 expect now, there's no logcat entry. We'll 337 00:12:15,130 --> 00:12:18,430 wait till it initializes. Alright, I can see 338 00:12:18,430 --> 00:12:19,630 we've got the three entries showing 339 00:12:19,630 --> 00:12:21,250 there, from our most recent call, and if I 340 00:12:21,250 --> 00:12:23,170 just go back to our emulator again and 341 00:12:23,170 --> 00:12:27,640 if I just run this again. So there's some 342 00:12:27,640 --> 00:12:29,080 sort of temporary issue there, probably 343 00:12:29,080 --> 00:12:30,310 again, relating to the fact this is a 344 00:12:30,310 --> 00:12:32,740 beta version of Android Studio where 345 00:12:32,740 --> 00:12:34,570 it's not doing the right thing. You can 346 00:12:34,570 --> 00:12:35,680 see we've now got the log entries 347 00:12:35,680 --> 00:12:37,600 working again. So what I want to do is 348 00:12:37,600 --> 00:12:40,360 just do that last bit of, that 349 00:12:40,360 --> 00:12:41,590 last testing and just to show that it 350 00:12:41,590 --> 00:12:44,080 is working. So if I type in these three 351 00:12:44,080 --> 00:12:47,230 entries again, so bread, milk and 352 00:12:47,230 --> 00:12:54,820 cheese, cheese, and if I type cream, but this 353 00:12:54,820 --> 00:12:56,200 time I'm not going to add that and I'm 354 00:12:56,200 --> 00:12:59,440 going to lock the device. So you can see 355 00:12:59,440 --> 00:13:01,540 the device was locked. We had on 356 00:13:01,540 --> 00:13:04,030 Pause called, onSaveInstanceState 357 00:13:04,030 --> 00:13:05,920 called and onStop called. 358 00:13:05,920 --> 00:13:08,140 So hopefully now, when i unlock it, we 359 00:13:08,140 --> 00:13:09,460 should see what I originally wanted to 360 00:13:09,460 --> 00:13:12,610 show you, was the onRestoreInstanceState not 361 00:13:12,610 --> 00:13:14,230 being called but being able to see the 362 00:13:14,230 --> 00:13:16,420 other entry. So let's try that, and you 363 00:13:16,420 --> 00:13:17,350 can see there, that's really what I 364 00:13:17,350 --> 00:13:19,060 wanted to show you early on, so clearly 365 00:13:19,060 --> 00:13:21,250 that was a bug with Android Studio. So 366 00:13:21,250 --> 00:13:23,920 onSaveInstanceState was called prior 367 00:13:23,920 --> 00:13:25,450 to us locking the screen, as well as on 368 00:13:25,450 --> 00:13:27,940 Stop and then, of course, previously onPause, 369 00:13:27,940 --> 00:13:30,580 but once we came back after unlocking 370 00:13:30,580 --> 00:13:32,950 the screen, we only got onRestart, on 371 00:13:32,950 --> 00:13:34,870 Start and onResume. We didn't get the on 372 00:13:34,870 --> 00:13:37,140 RestoreInstanceState being called, 373 00:13:37,140 --> 00:13:39,490 because unless Android does destroy 374 00:13:39,490 --> 00:13:40,660 the activity, there's no need to 375 00:13:40,660 --> 00:13:42,610 actually call that onRestoreInstance 376 00:13:42,610 --> 00:13:44,470 State. And again, if that was true we'd 377 00:13:44,470 --> 00:13:46,330 expect to still see our list, and cream 378 00:13:46,330 --> 00:13:47,860 in the editText when we unlocked 379 00:13:47,860 --> 00:13:49,780 the device, and you can see here, that 380 00:13:49,780 --> 00:13:51,070 clearly, it hasn't been destroyed because 381 00:13:51,070 --> 00:13:53,560 we can still see those items. Alright, 382 00:13:53,560 --> 00:13:54,880 so we got there in the end. We had a few 383 00:13:54,880 --> 00:13:56,380 problems with the software but we 384 00:13:56,380 --> 00:13:58,210 managed to get it working. 385 00:13:58,210 --> 00:14:00,520 So that's the activity lifecycle. Now 386 00:14:00,520 --> 00:14:02,350 that technique, adding logging to all the 387 00:14:02,350 --> 00:14:04,029 methods, is great for seeing what's going 388 00:14:04,029 --> 00:14:06,399 on and also when various methods are 389 00:14:06,399 --> 00:14:08,140 called. It doesn't just apply to the 390 00:14:08,140 --> 00:14:10,270 lifecycle methods either, because there's 391 00:14:10,270 --> 00:14:11,950 all sorts of situations where we have to 392 00:14:11,950 --> 00:14:13,720 provide callbacks for Android to call. 393 00:14:13,720 --> 00:14:16,089 And adding logging to them so you can 394 00:14:16,089 --> 00:14:18,279 see exactly when Android does call them, 395 00:14:18,279 --> 00:14:19,959 is very good way to develop your 396 00:14:19,959 --> 00:14:22,330 understanding of how Android works. Al 397 00:14:22,330 --> 00:14:23,620 right, so there's a few important 398 00:14:23,620 --> 00:14:25,209 implications of what we've looked at 399 00:14:25,209 --> 00:14:27,670 with the activity lifecycle. We're going 400 00:14:27,670 --> 00:14:31,140 to discuss those in the next video.