1 00:00:00,339 --> 00:00:02,659 (vibrant music) 2 00:00:02,659 --> 00:00:05,340 (typing) 3 00:00:05,340 --> 00:00:08,080 Alright so we've got the app navigation working. 4 00:00:08,080 --> 00:00:09,460 Let's have a look at what we're gonna do 5 00:00:09,460 --> 00:00:11,310 when the back button's tapped. 6 00:00:11,310 --> 00:00:13,920 Reacting to the back button's really easy. 7 00:00:13,920 --> 00:00:17,310 Activities have an onBackPressed function that gets called. 8 00:00:17,310 --> 00:00:19,619 What we have to do to start with is decide how 9 00:00:19,619 --> 00:00:21,770 our app's going to respond to it. 10 00:00:21,770 --> 00:00:23,220 So let's get back to the app 11 00:00:23,220 --> 00:00:25,630 and start by seeing what happens at the moment, 12 00:00:25,630 --> 00:00:27,390 when we just rely on the default behaviour. 13 00:00:27,390 --> 00:00:29,303 So we're gonna bring up the emulator again. 14 00:00:31,420 --> 00:00:32,790 And you can see the emulator's in landscape. 15 00:00:32,790 --> 00:00:33,623 So we're gonna start there. 16 00:00:33,623 --> 00:00:35,300 So we'll just start the app again. 17 00:00:37,050 --> 00:00:38,050 It's been backed up. 18 00:00:41,080 --> 00:00:45,102 So if I tap the back button now, from the main screen, 19 00:00:45,102 --> 00:00:47,230 you can see it's just closed the app, 20 00:00:47,230 --> 00:00:49,990 which is exactly what the user would expect to happen. 21 00:00:49,990 --> 00:00:51,630 And the same will happen in portrait. 22 00:00:51,630 --> 00:00:52,940 And that's fine. 23 00:00:52,940 --> 00:00:54,490 So we'll run the app again now. 24 00:00:59,630 --> 00:01:02,163 This time I'm gonna click on the plus icon. 25 00:01:03,370 --> 00:01:05,340 Now we've got a different situation. 26 00:01:05,340 --> 00:01:08,530 What will the user expect back to do here? 27 00:01:08,530 --> 00:01:10,060 If they'd added some data, 28 00:01:10,060 --> 00:01:11,530 the very least we should do is ask 29 00:01:11,530 --> 00:01:14,240 for confirmation that they wanna quit editing. 30 00:01:14,240 --> 00:01:15,770 And we'll be looking at dialogues later 31 00:01:15,770 --> 00:01:17,660 and we'll see how to do that. 32 00:01:17,660 --> 00:01:20,430 If they choose to quit, should we exit the app 33 00:01:20,430 --> 00:01:22,370 or just remove the fragment? 34 00:01:22,370 --> 00:01:25,320 I personally probably want the app to exit if I was 35 00:01:25,320 --> 00:01:28,260 in this position as a user, so that's what we'll do. 36 00:01:28,260 --> 00:01:30,600 The situation's different in portrait though. 37 00:01:30,600 --> 00:01:32,680 We still got the fragment showing. 38 00:01:32,680 --> 00:01:34,980 So I rotate the device back into portrait now. 39 00:01:38,130 --> 00:01:39,760 So this is a bit different. 40 00:01:39,760 --> 00:01:42,480 As a user, I'd expect the back button 41 00:01:42,480 --> 00:01:44,210 to do the same as up from here. 42 00:01:44,210 --> 00:01:46,650 In other words, take me back to the main screen. 43 00:01:46,650 --> 00:01:49,210 That seems more expected, so that's what we'll do. 44 00:01:49,210 --> 00:01:51,780 Notice, if I press back at the moment, 45 00:01:51,780 --> 00:01:52,993 it just exits the app. 46 00:01:54,190 --> 00:01:56,490 Right, so let's get back to main activity. 47 00:01:56,490 --> 00:01:58,770 And what we need to do is add the onBackPressed function 48 00:01:58,770 --> 00:02:00,090 to main activity. 49 00:02:00,090 --> 00:02:02,573 And I'll do that towards the end of the file. 50 00:02:05,270 --> 00:02:07,380 Just before these life cycle functions, 51 00:02:07,380 --> 00:02:08,830 so I'll just do it down here. 52 00:02:10,000 --> 00:02:12,370 So let's get to override a method. 53 00:02:12,370 --> 00:02:15,760 And the method we wanna override was onBackPressed. 54 00:02:15,760 --> 00:02:18,115 So I've grabbed that one. 55 00:02:18,115 --> 00:02:19,863 We entered the default code for it. 56 00:02:21,350 --> 00:02:22,580 Alright, and it's that code 57 00:02:22,580 --> 00:02:24,670 to the super dock onBackPressed function, 58 00:02:24,670 --> 00:02:27,190 that's causing that default behaviour. 59 00:02:27,190 --> 00:02:29,270 So basically calls another function, 60 00:02:29,270 --> 00:02:32,710 the activity's finished function to close down the activity. 61 00:02:32,710 --> 00:02:35,590 We're gonna check to see if the added at fragment exists 62 00:02:35,590 --> 00:02:37,840 and changed the behaviour if it does. 63 00:02:37,840 --> 00:02:39,340 So let's start with some code. 64 00:02:40,400 --> 00:02:42,700 So I'll start by val fragment 65 00:02:45,150 --> 00:02:47,770 equals supportFragmentManager 66 00:02:47,770 --> 00:02:51,910 period findFragmentById parenthesis R dot ID dot 67 00:02:51,910 --> 00:02:56,123 task_details_container, which is our edited fragment. 68 00:02:57,170 --> 00:02:58,620 And then we'll do a test. 69 00:02:58,620 --> 00:03:01,080 If fragment is equal to null 70 00:03:02,120 --> 00:03:07,120 or bring nTwoPane, nTwoPane mode, then and only then 71 00:03:10,140 --> 00:03:12,740 will we call the super to onBackPressed. 72 00:03:14,590 --> 00:03:16,140 Otherwise, we put an else here. 73 00:03:16,990 --> 00:03:19,970 What we'll do instead is we'll removeEditPane 74 00:03:19,970 --> 00:03:21,470 and pass our fragment to that. 75 00:03:22,630 --> 00:03:24,740 So if we're not showing the fragment or 76 00:03:24,740 --> 00:03:27,470 if we're in twoPane mode then we're calling the super dock 77 00:03:27,470 --> 00:03:29,760 onBackPressed function to exit normally. 78 00:03:29,760 --> 00:03:32,040 Otherwise, we're just removing the fragment 79 00:03:32,040 --> 00:03:34,590 using our removeEditPane function. 80 00:03:34,590 --> 00:03:36,570 So as I said we'll be adding a dialogue later 81 00:03:36,570 --> 00:03:39,290 to get confirmation that the user's happy to lose any 82 00:03:39,290 --> 00:03:41,840 changes there that you may have made when editing. 83 00:03:41,840 --> 00:03:44,180 For now though, see how this works. 84 00:03:44,180 --> 00:03:45,380 We'll run the app again. 85 00:03:52,000 --> 00:03:53,320 And so my emulator's in portrait, 86 00:03:53,320 --> 00:03:54,153 so I'll start there. 87 00:03:54,153 --> 00:03:57,750 So I'm gonna click on the plus icon to display the fragment, 88 00:03:57,750 --> 00:04:00,370 then we'll click on the back button. 89 00:04:00,370 --> 00:04:03,140 So that now looks like what users will expect. 90 00:04:03,140 --> 00:04:04,907 The app just returns to the main screen. 91 00:04:04,907 --> 00:04:08,870 And if I press back again, the app exits normally. 92 00:04:08,870 --> 00:04:10,670 Alright, so let's run the app again. 93 00:04:15,270 --> 00:04:17,360 We're gonna tap the plus icon 94 00:04:17,360 --> 00:04:19,310 and we're gonna rotate it to landscape. 95 00:04:21,990 --> 00:04:25,350 Because the main screen is showing the user, 96 00:04:25,350 --> 00:04:28,000 we'll probably expect to close the app. 97 00:04:28,000 --> 00:04:30,660 Of course you might disagree with this, and that's fine. 98 00:04:30,660 --> 00:04:32,830 If you want the screen to behave the same as portrait 99 00:04:32,830 --> 00:04:34,260 and just remove the fragment, 100 00:04:34,260 --> 00:04:36,980 then you can just remove the task for nTwoPane 101 00:04:36,980 --> 00:04:38,500 in our onBackPressed function. 102 00:04:38,500 --> 00:04:40,400 It's up to you how your app behaves. 103 00:04:40,400 --> 00:04:42,940 I'm sure you had achieved whatever you decide. 104 00:04:42,940 --> 00:04:46,103 Alright so let's tap back now to see what happens. 105 00:04:47,470 --> 00:04:50,030 And in our case, because of the way we've coded it, 106 00:04:50,030 --> 00:04:51,800 the app exits. 107 00:04:51,800 --> 00:04:52,900 And we should also make sure that 108 00:04:52,900 --> 00:04:54,350 we haven't broken the app button. 109 00:04:54,350 --> 00:04:55,950 So let's just run the app again. 110 00:05:00,010 --> 00:05:01,683 And tap on plus again. 111 00:05:03,390 --> 00:05:06,340 So the up button here in landscape doesn't quit, 112 00:05:06,340 --> 00:05:07,330 it just removes the fragment. 113 00:05:07,330 --> 00:05:10,470 Well it does what it should do, so let's confirm that. 114 00:05:10,470 --> 00:05:12,867 That's correct, it has done that. 115 00:05:12,867 --> 00:05:14,353 And let's go back to portrait. 116 00:05:16,800 --> 00:05:21,670 Plus, click on up, and it successfully removes the fragment 117 00:05:21,670 --> 00:05:23,810 and it doesn't try and exit. 118 00:05:23,810 --> 00:05:25,700 So up will remove the fragment as you saw 119 00:05:25,700 --> 00:05:27,630 and there's no further testing needed and 120 00:05:27,630 --> 00:05:30,020 that's because the up button's been removed from the toolbar 121 00:05:30,020 --> 00:05:32,860 in both landscape and portrait testing there. 122 00:05:32,860 --> 00:05:34,850 Alright so that's our up and back navigation 123 00:05:34,850 --> 00:05:36,260 finished for now. 124 00:05:36,260 --> 00:05:38,540 And we'll be looking at dialogues later, 125 00:05:38,540 --> 00:05:41,290 as I mentioned and giving the user a chance to confirm 126 00:05:41,290 --> 00:05:44,600 that they wanna quit but I'll stop this video here for now. 127 00:05:44,600 --> 00:05:46,800 The next step in our app is to save the data 128 00:05:46,800 --> 00:05:48,680 that the user's entered or changed. 129 00:05:48,680 --> 00:05:50,920 So it's time to start hitting the database. 130 00:05:50,920 --> 00:05:52,920 As I mentioned in the view model section, 131 00:05:52,920 --> 00:05:55,660 Google have now deprecated the loader classes. 132 00:05:55,660 --> 00:05:57,920 We'd originally used the loader to provide data 133 00:05:57,920 --> 00:05:59,420 for our main activity. 134 00:05:59,420 --> 00:06:01,890 Now we're going to use it to view model instead. 135 00:06:01,890 --> 00:06:04,065 When writing your apps, you'll often get to a point 136 00:06:04,065 --> 00:06:06,020 where something you wanna do relies 137 00:06:06,020 --> 00:06:07,620 on something you haven't written yet 138 00:06:07,620 --> 00:06:09,240 and that's the case here. 139 00:06:09,240 --> 00:06:11,810 Our view model class will take care of presenting the data 140 00:06:11,810 --> 00:06:14,840 to main activity as well as saving the changes. 141 00:06:14,840 --> 00:06:17,010 But we haven't written that yet. 142 00:06:17,010 --> 00:06:18,860 Obviously we can't present data to display 143 00:06:18,860 --> 00:06:21,110 if there isn't any saved in the database. 144 00:06:21,110 --> 00:06:22,330 So they get around that. 145 00:06:22,330 --> 00:06:25,920 I'm going to get the edited fragment to save the data. 146 00:06:25,920 --> 00:06:27,270 This is only temporary. 147 00:06:27,270 --> 00:06:29,440 And we'll be moving the code into the view model 148 00:06:29,440 --> 00:06:31,800 but it's quite common to do things like this. 149 00:06:31,800 --> 00:06:34,930 If we put the code that saves the task into its own function 150 00:06:34,930 --> 00:06:37,480 moving it shouldn't result in not too many changes 151 00:06:37,480 --> 00:06:39,200 when we get to that stage of moving it. 152 00:06:39,200 --> 00:06:40,033 So that's what we'll do 153 00:06:40,033 --> 00:06:42,290 and we'll start work on that in the next video.