1 00:00:00,610 --> 00:00:03,960 All right, let's switch to DetailActivity and handle the data on that side. 2 00:00:05,790 --> 00:00:07,836 Let's comment out these few lines here because we aren't going to 3 00:00:07,836 --> 00:00:08,426 use them anymore. 4 00:00:08,426 --> 00:00:09,960 I'll just leave them here as reference. 5 00:00:11,460 --> 00:00:13,160 So we still want to use a null check but 6 00:00:13,160 --> 00:00:15,562 this time we're going to call a different intent method. 7 00:00:15,562 --> 00:00:21,650 If intent.get, and here we want to use the getParcelableExtra, 8 00:00:21,650 --> 00:00:23,530 since we're passing in parcelable data. 9 00:00:24,580 --> 00:00:28,626 We passed in with MainActivity.EXTRA_SONG. 10 00:00:28,626 --> 00:00:31,450 And we want to make sure that this is not equal to null. 11 00:00:33,990 --> 00:00:36,670 Okay, so now inside here, it's not null, so 12 00:00:36,670 --> 00:00:39,148 we can use that same line to initialize a song variable. 13 00:00:39,148 --> 00:00:42,433 Song song = and I'm going to copy and paste. 14 00:00:45,500 --> 00:00:47,590 And here we go, now we have a song variable. 15 00:00:49,020 --> 00:00:50,890 Now we can set the title from this variable. 16 00:00:50,890 --> 00:00:55,681 So titleLabel.setText, and we can use song.getTitle, and we can also 17 00:00:55,681 --> 00:01:00,560 initialize our favorite check box because that is part of the song object. 18 00:01:00,560 --> 00:01:07,492 So we can say favoriteCheckbox.setChecked and we can use song.isFavorite. 19 00:01:07,492 --> 00:01:08,630 We need one more thing. 20 00:01:08,630 --> 00:01:12,030 Remember that we are tracking the position of the song that was tapped on. 21 00:01:12,030 --> 00:01:13,840 We can get it as an int ListPosition. 22 00:01:13,840 --> 00:01:16,066 I'm actually gonna put that outside this if statement. 23 00:01:16,066 --> 00:01:22,920 So, int ListPosition = intent.getIntExtra. 24 00:01:22,920 --> 00:01:27,160 And the name was MainActivity.EXTRA_LIST_POSITION. 25 00:01:27,160 --> 00:01:30,030 And the getInt method requires a default value. 26 00:01:30,030 --> 00:01:31,650 This is why we don't need to put it inside of a check. 27 00:01:31,650 --> 00:01:35,190 So if it's not there, we'll just use a default and we should use 0 for a list. 28 00:01:37,390 --> 00:01:39,620 To use this list position back in MainActivity, 29 00:01:39,620 --> 00:01:42,300 we want to pass it back as a result in our result intent. 30 00:01:43,490 --> 00:01:45,792 So here where we're putting extra data, 31 00:01:45,792 --> 00:01:48,590 let's set on our line result intent.putExtra. 32 00:01:48,590 --> 00:01:51,959 And we'll use the same key, MainActivity.EXTRA_LIST_POSITION, and 33 00:01:51,959 --> 00:01:53,330 we'll pass in listPosition. 34 00:01:55,820 --> 00:01:59,350 Notice that Android Studio automatically added the final keyword, which we need 35 00:01:59,350 --> 00:02:02,830 when we're referencing the variable inside of an anonymous class like we are here. 36 00:02:04,270 --> 00:02:07,260 The last piece of this puzzle is back in MainActivity. 37 00:02:07,260 --> 00:02:10,070 We want to show the favorite heart if the song is checked. 38 00:02:10,070 --> 00:02:13,290 So now instead of just logging the boolean result down here, 39 00:02:13,290 --> 00:02:15,570 let's use it to update the view. 40 00:02:15,570 --> 00:02:20,930 First we need the position in the list int position = and our intent is called data. 41 00:02:20,930 --> 00:02:23,881 And like before, we can call getIntExtra, 42 00:02:23,881 --> 00:02:27,805 pass in EXTRA_LIST_POSITION and a default value of 0. 43 00:02:27,805 --> 00:02:33,040 Now, how can we update an individual view in a separate recycler view? 44 00:02:33,040 --> 00:02:37,140 The solution is to update the underlying data and then notify the adapter that 45 00:02:37,140 --> 00:02:41,250 the data has been changed, and that the item should be bound to the view again. 46 00:02:41,250 --> 00:02:44,185 We can update the data using the Playlist class, 47 00:02:44,185 --> 00:02:48,250 Playlist.songs, and this is an array, so we can pass in the position, 48 00:02:48,250 --> 00:02:52,575 and then we can set the favorite value with setIsFavorite. 49 00:02:53,810 --> 00:02:55,400 And we have the value stored in result. 50 00:02:57,340 --> 00:03:00,260 To notify the adapter of a change at a specific position, 51 00:03:00,260 --> 00:03:06,250 we'll call mAdapter.notifyItemChanged at a certain position. 52 00:03:07,800 --> 00:03:09,210 Now we can run this to see it in action. 53 00:03:13,490 --> 00:03:15,290 So first we need to tap on a song in the list. 54 00:03:15,290 --> 00:03:18,870 I'll tap on this one, Live and Die, and it's going to open up. 55 00:03:18,870 --> 00:03:20,424 I'm going to check it as a favorite. 56 00:03:22,494 --> 00:03:25,924 And when we return to the MainActivity, there we go, 57 00:03:25,924 --> 00:03:27,990 we see our favorite heart icon. 58 00:03:29,250 --> 00:03:31,830 Now if we go back in, let's tap on Live and Die again. 59 00:03:33,350 --> 00:03:36,110 We'll uncheck the checkbox, and we'll return. 60 00:03:38,140 --> 00:03:39,610 And the heart icon disappears. 61 00:03:41,600 --> 00:03:45,320 Let's take another look at that table of different types of communication. 62 00:03:45,320 --> 00:03:48,740 We've fully covered how to pass both simple and complex data back and 63 00:03:48,740 --> 00:03:52,760 forth between activities, using intents and extras. 64 00:03:52,760 --> 00:03:54,900 Well, it still looks like we have a lot to cover. 65 00:03:54,900 --> 00:03:58,330 Understanding this foundation makes the rest of these pretty easy. 66 00:03:58,330 --> 00:04:01,250 It's the same basic idea over and over. 67 00:04:01,250 --> 00:04:04,670 Share information as extra data on an intent object, and 68 00:04:04,670 --> 00:04:06,760 then make sure that extra data is handled somehow. 69 00:04:08,110 --> 00:04:11,500 Remember, everything we've covered for activities works the same way for 70 00:04:11,500 --> 00:04:13,630 communicating to and from services too. 71 00:04:15,300 --> 00:04:16,680 Nice work so far. 72 00:04:16,680 --> 00:04:20,030 I now intend to take a short break, and I'll make it explicit 73 00:04:20,030 --> 00:04:24,070 by naming the activity that will handle my intention, playing a game. 74 00:04:24,070 --> 00:04:25,780 You take a break, too, but come back soon, 75 00:04:25,780 --> 00:04:29,250 because I wanna get back to those implicit intents we touched on earlier. 76 00:04:30,440 --> 00:04:32,450 In fact, if you'd like, 77 00:04:32,450 --> 00:04:36,640 why don't you try creating an implicit intent to share location on a map? 78 00:04:36,640 --> 00:04:39,250 I'll leave a link in the teacher's notes and you can try it out, and 79 00:04:39,250 --> 00:04:41,960 then we can look at it together in the next section. 80 00:04:41,960 --> 00:04:43,960 Be sure to watch even if you get it working, 81 00:04:43,960 --> 00:04:46,340 because I want to show you some special cases. 82 00:04:46,340 --> 00:04:46,840 Good luck.