1 00:00:00,018 --> 00:00:02,601 (bright music) 2 00:00:05,500 --> 00:00:07,890 Okay, so we need to remove the up button 3 00:00:07,890 --> 00:00:09,120 from the toolbar. 4 00:00:09,120 --> 00:00:10,950 We could do that in undetach 5 00:00:10,950 --> 00:00:14,400 but I think it's safer to let main activity handle this. 6 00:00:14,400 --> 00:00:16,870 It's fine to have a fragment at the up button 7 00:00:16,870 --> 00:00:18,690 because it knows that it's needed. 8 00:00:18,690 --> 00:00:20,890 But there are priority at beam one. 9 00:00:20,890 --> 00:00:23,540 In that case, we don't want a fragment to go removing it. 10 00:00:23,540 --> 00:00:26,940 The fragment's got no idea what else the activity is doing 11 00:00:26,940 --> 00:00:29,080 nor whether its activity was navigated to 12 00:00:29,080 --> 00:00:30,840 from another activity. 13 00:00:30,840 --> 00:00:33,010 If our fragment removes the up button, 14 00:00:33,010 --> 00:00:36,610 it may break the navigation back from its own activity. 15 00:00:36,610 --> 00:00:38,840 Main activity already removes the fragment 16 00:00:38,840 --> 00:00:42,060 and that's a good place to also remove the up button. 17 00:00:42,060 --> 00:00:43,170 So let's have a look at doing that 18 00:00:43,170 --> 00:00:44,243 in the removeEditPane function. 19 00:00:47,727 --> 00:00:51,016 We'll do that as the last statement in that function. 20 00:00:51,016 --> 00:00:53,027 I'm gonna type supportActionBar 21 00:00:55,160 --> 00:00:59,863 question mark dot setdisplayHomeAsUpEnabled false. 22 00:01:01,120 --> 00:01:04,230 Now remember that we have to use the supportActionBar here. 23 00:01:04,230 --> 00:01:07,760 If you use ActionBar instead, the code will still run 24 00:01:07,760 --> 00:01:09,670 but the button won't be removed. 25 00:01:09,670 --> 00:01:11,050 Our app doesn't have an ActionBar. 26 00:01:11,050 --> 00:01:14,990 It's using the support ActionBar from the support library. 27 00:01:14,990 --> 00:01:17,850 Alright, so that code on line 62, will remove the up button. 28 00:01:17,850 --> 00:01:19,800 We just now need to call this function 29 00:01:19,800 --> 00:01:21,670 when the button's tapped. 30 00:01:21,670 --> 00:01:23,260 And we do that just like we respond 31 00:01:23,260 --> 00:01:26,310 to any of the other toolbar icons in the on options, 32 00:01:26,310 --> 00:01:28,480 item selected function. 33 00:01:28,480 --> 00:01:30,530 So let's just go down and check that out. 34 00:01:32,717 --> 00:01:33,680 Then let's add some code. 35 00:01:33,680 --> 00:01:36,730 We'll have it starting on line 85. 36 00:01:36,730 --> 00:01:38,786 So that's gonna be android 37 00:01:38,786 --> 00:01:43,310 .R.id.home 38 00:01:44,300 --> 00:01:46,390 and dot period, when I say that it means the same thing 39 00:01:46,390 --> 00:01:48,140 in case I haven't mentioned that before. 40 00:01:48,140 --> 00:01:50,940 Or any of that dash, greater than sign again. 41 00:01:50,940 --> 00:01:53,880 Adding a block of code there, left and right curly braces. 42 00:01:53,880 --> 00:01:55,890 We'll stop by logging, so log.d 43 00:01:55,890 --> 00:01:59,273 parenthesis tag comma onOptionsItemSelected 44 00:02:01,858 --> 00:02:03,563 colon home button pressed. 45 00:02:06,700 --> 00:02:11,700 Next line, val fragment is equal to supportFragmentManager 46 00:02:12,350 --> 00:02:14,440 dot findFragmentById. 47 00:02:14,440 --> 00:02:16,140 In parenthesis it's gonna be R.id. 48 00:02:17,420 --> 00:02:21,630 and its task_details_container, closing parenthesis. 49 00:02:21,630 --> 00:02:25,400 Then the next line is gonna be removeEdit fragment, fragment 50 00:02:25,400 --> 00:02:27,920 and that's obviously calling the function that we just added 51 00:02:27,920 --> 00:02:30,093 the entry to remove the up button. 52 00:02:31,730 --> 00:02:36,160 The ID R.id.menumain_addTask 53 00:02:36,160 --> 00:02:38,220 which you can see on line 83. 54 00:02:38,220 --> 00:02:40,300 That's an ID we've used in our layout. 55 00:02:40,300 --> 00:02:42,780 The ID for the up button is provided by android, 56 00:02:42,780 --> 00:02:45,230 android.R.id.home. 57 00:02:45,230 --> 00:02:47,140 So we respond to it being tapped 58 00:02:47,140 --> 00:02:49,820 just like any other menu item and we're then using 59 00:02:49,820 --> 00:02:52,690 our removeEditPane function to remove the fragment. 60 00:02:52,690 --> 00:02:55,100 I've locked the home button being pressed 61 00:02:55,100 --> 00:02:57,030 because that's what it's called inside android 62 00:02:57,030 --> 00:02:58,460 as the ID indicates. 63 00:02:58,460 --> 00:03:00,280 This can be confusing because the phone 64 00:03:00,280 --> 00:03:03,920 also has a home button that returns to the home screen. 65 00:03:03,920 --> 00:03:07,090 While I'm in main activity, there's a couple typos to fix. 66 00:03:07,090 --> 00:03:09,210 We did put a banner message up in an earlier video. 67 00:03:09,210 --> 00:03:11,270 So you probably have already corrected them. 68 00:03:11,270 --> 00:03:13,010 But it's in the onCreate function 69 00:03:13,010 --> 00:03:14,460 towards the top of the class. 70 00:03:16,490 --> 00:03:18,680 We need to fix this because I've got onCreated finished, 71 00:03:18,680 --> 00:03:21,653 it should be onCreate starts. 72 00:03:25,000 --> 00:03:26,720 Okay, and there's a couple of warnings here 73 00:03:26,720 --> 00:03:27,620 to fix as well. 74 00:03:27,620 --> 00:03:30,400 Well, there's actually one in this particular function. 75 00:03:30,400 --> 00:03:32,970 You can see you've got a reference to var there, 76 00:03:32,970 --> 00:03:34,410 that should be declared as val 77 00:03:34,410 --> 00:03:36,868 or can be declared as val instead of var. 78 00:03:36,868 --> 00:03:38,920 That will fix that warning. 79 00:03:38,920 --> 00:03:41,010 Alright, so that's so many to fix there. 80 00:03:41,010 --> 00:03:44,623 The second one was in our onSaveClicked function. 81 00:03:46,020 --> 00:03:47,856 I've also used a var for that. 82 00:03:47,856 --> 00:03:50,450 We can use a val for that, as the warning indicates. 83 00:03:50,450 --> 00:03:53,670 We'll do that, and that fixes that warning up as well. 84 00:03:53,670 --> 00:03:56,260 Alright, time now to see if it works. 85 00:03:56,260 --> 00:03:58,740 So before I do that, we need to go into our 86 00:04:00,140 --> 00:04:02,170 Android Virtual Devices. 87 00:04:02,170 --> 00:04:06,850 We need to create a new emulator for API 28. 88 00:04:06,850 --> 00:04:08,320 I'm gonna select Nexus 5X again 89 00:04:08,320 --> 00:04:10,240 because that's what I've used previously. 90 00:04:10,240 --> 00:04:13,360 I'm gonna make sure I select the pie, API level 28. 91 00:04:13,360 --> 00:04:15,040 So using the latest version. 92 00:04:15,040 --> 00:04:16,570 That's where our code is building against now, 93 00:04:16,570 --> 00:04:17,980 that's the version we've specified 94 00:04:17,980 --> 00:04:19,370 in the build.greater file. 95 00:04:19,370 --> 00:04:21,370 Click on Finish. 96 00:04:21,370 --> 00:04:22,670 That's our API 28. 97 00:04:22,670 --> 00:04:25,060 And it didn't really matter whether we chose the Google Play 98 00:04:25,060 --> 00:04:29,100 or the other version the Google API version. 99 00:04:29,100 --> 00:04:30,520 I'm just gonna use the play version for now 100 00:04:30,520 --> 00:04:31,850 and will kinda start that. 101 00:04:31,850 --> 00:04:33,400 You can close down this window. 102 00:04:37,730 --> 00:04:38,763 And that boot up. 103 00:04:42,879 --> 00:04:46,790 I'll make that a bit bigger so we can see what we're doing. 104 00:04:48,250 --> 00:04:50,500 So there's our emulator started up. 105 00:04:50,500 --> 00:04:51,800 Let's actually run the app 106 00:04:54,690 --> 00:04:56,363 on the API 28 device. 107 00:04:59,920 --> 00:05:01,040 Alright, so the app's working okay. 108 00:05:01,040 --> 00:05:02,950 Let's click on the little plus icon 109 00:05:02,950 --> 00:05:04,600 and you wanna be in the portrait view for this, 110 00:05:04,600 --> 00:05:05,850 which we are. 111 00:05:05,850 --> 00:05:07,700 The up button appears in the toolbar as you can see 112 00:05:07,700 --> 00:05:10,410 and we're tapping it back now. 113 00:05:10,410 --> 00:05:11,883 As I'm tapping the up button, 114 00:05:12,820 --> 00:05:15,240 successfully takes us back to the main screen. 115 00:05:15,240 --> 00:05:17,780 Notice now that the up button is removed as well 116 00:05:17,780 --> 00:05:18,613 so that's good. 117 00:05:18,613 --> 00:05:21,320 We're gonna tap the button again now 118 00:05:21,320 --> 00:05:23,473 and this time navigate over into landscape. 119 00:05:25,936 --> 00:05:28,740 You need to tap this little button down here 120 00:05:28,740 --> 00:05:31,590 which then actually causes the emulator 121 00:05:31,590 --> 00:05:33,530 to go into landscape mode. 122 00:05:33,530 --> 00:05:34,970 Now the words, it's no longer updating 123 00:05:34,970 --> 00:05:37,120 the screen automatically which is a bit annoying. 124 00:05:37,120 --> 00:05:38,640 But one of the things you can do to fix that 125 00:05:38,640 --> 00:05:40,290 is you can drag down the settings 126 00:05:42,160 --> 00:05:43,940 and click on this little icon here, 127 00:05:43,940 --> 00:05:45,650 the square arrowed icons. 128 00:05:45,650 --> 00:05:49,110 You click on that, that goes back to changing automatically 129 00:05:49,110 --> 00:05:50,740 which is what we want here. 130 00:05:50,740 --> 00:05:51,573 So I'll get rid of that. 131 00:05:51,573 --> 00:05:54,240 And just to confirm that, I go back to portrait, 132 00:05:54,240 --> 00:05:57,370 back to landscape and it's now moving for us, 133 00:05:57,370 --> 00:05:59,770 moving to a landscape mode automatically for us. 134 00:06:00,690 --> 00:06:03,600 Again, we've got at the moment now that we've 135 00:06:03,600 --> 00:06:04,840 had moved into landscape mode. 136 00:06:04,840 --> 00:06:07,010 We've still got the up button showing in the top, 137 00:06:07,010 --> 00:06:08,630 upper left-hand corner which is good. 138 00:06:08,630 --> 00:06:12,790 And tapping it, removes the fragment which is good. 139 00:06:12,790 --> 00:06:15,230 So that's working and clicking on that, 140 00:06:15,230 --> 00:06:17,570 one more time the plus icon, 141 00:06:17,570 --> 00:06:19,820 and up, definitely closes the fragment 142 00:06:19,820 --> 00:06:21,690 so that's actually working well. 143 00:06:21,690 --> 00:06:22,920 Let's finish the video here. 144 00:06:22,920 --> 00:06:25,360 In the next one, we'll have a look at what we should do 145 00:06:25,360 --> 00:06:27,920 when the back button is pressed as opposed to the up button 146 00:06:27,920 --> 00:06:28,830 that we've been working on. 147 00:06:28,830 --> 00:06:30,503 I'll see you in the next video.