1
00:00:05,100 --> 00:00:06,100
Welcome back.

2
00:00:06,800 --> 00:00:08,800
In September 2019,

3
00:00:09,100 --> 00:00:12,600
Google released version 1.0.0

4
00:00:12,600 --> 00:00:16,600
of two libraries that can make working with activities and fragments easier

5
00:00:17,100 --> 00:00:18,900
when using jetpack components.

6
00:00:19,700 --> 00:00:23,900
To use them, we'll add another dependency to our build.gradle file,

7
00:00:24,100 --> 00:00:26,300
the module one not the project one.

8
00:00:52,100 --> 00:00:55,500
The current release of this library is now 1.2.4

9
00:00:56,000 --> 00:00:58,200
but a later version may be available for you.

10
00:00:58,860 --> 00:01:01,860
A good place to learn about these jetpack libraries

11
00:01:01,860 --> 00:01:05,860
and keep track of the version is Google's jetpack pages.

12
00:01:09,360 --> 00:01:12,060
It's worth bookmarking the main jetpack page

13
00:01:12,060 --> 00:01:15,060
then checking regularly to see what's changed.

14
00:01:15,610 --> 00:01:17,810
Jetpack has been continually developed

15
00:01:18,110 --> 00:01:21,310
and new libraries and features are being added all the time.

16
00:01:22,510 --> 00:01:25,110
The note, a short way down the page is important.

17
00:01:25,510 --> 00:01:28,170
We can't compile code that uses this library

18
00:01:28,170 --> 00:01:30,370
if we're compiling to java 7.

19
00:01:30,870 --> 00:01:32,670
We have to specify java 8.

20
00:01:33,570 --> 00:01:37,270
We do that by adding an entry in the android section

21
00:01:37,270 --> 00:01:38,470
of build.gradle.

22
00:02:01,370 --> 00:02:04,370
Make sure you sync now before moving on.

23
00:02:10,669 --> 00:02:12,670
Okay. How does that help us?

24
00:02:13,570 --> 00:02:17,070
It lets us simplify the way we create our view model instances.

25
00:02:17,870 --> 00:02:19,970
I'll change both activities first

26
00:02:20,270 --> 00:02:23,870
because I want to draw your attention to an important difference with fragments.

27
00:03:10,670 --> 00:03:13,870
The change in durations report is the same.

28
00:03:50,120 --> 00:03:51,720
Be careful with your fragments.

29
00:03:52,420 --> 00:03:55,020
If you use exactly the same code in fragment,

30
00:03:55,380 --> 00:03:59,630
it's equivalent to passing this to the ViewModelProvider constructor.

31
00:04:00,510 --> 00:04:03,010
If we want to share the activities view model,

32
00:04:03,010 --> 00:04:05,310
we have to use a slightly different form.

33
00:04:05,860 --> 00:04:08,460
I'll change AddEditFragment first.

34
00:04:49,460 --> 00:04:52,960
Note that we use by ActivityViewModels not ViewModels.

35
00:04:53,510 --> 00:04:56,810
That ensures that we get the instance of the ActivitiesViewModel

36
00:04:57,310 --> 00:05:01,010
equivalent to passing activity on the commented outline above.

37
00:05:02,110 --> 00:05:04,610
I'll make the same change in MainActivityFragment.

38
00:05:40,410 --> 00:05:42,410
While we're dealing with this library,

39
00:05:42,410 --> 00:05:45,010
switch back to the document we were just looking at.

40
00:05:49,260 --> 00:05:52,860
Scroll down to the version 1.2.0 section

41
00:05:53,060 --> 00:05:57,560
and have a look at the top important changes since 1.1.0.

42
00:05:58,660 --> 00:06:01,360
Google have added a FragmentContainerView

43
00:06:01,760 --> 00:06:04,760
which is now the strongly recommended container

44
00:06:05,120 --> 00:06:06,480
for hosting our fragments.

45
00:06:07,280 --> 00:06:12,180
If they strongly recommend it and we're still at the stage of testing and developing our app.

46
00:06:12,500 --> 00:06:14,000
It's a good idea to use one.

47
00:06:14,400 --> 00:06:16,280
Back in android studio

48
00:06:16,280 --> 00:06:19,280
edit the content_mainlayout

49
00:06:19,280 --> 00:06:20,780
in res/layouts.

50
00:06:33,180 --> 00:06:36,780
At the moment, this new container doesn't appear in the palette.

51
00:06:37,080 --> 00:06:39,850
I'm sure Google will add it in there at some point.

52
00:06:39,850 --> 00:06:43,510
But as we've already got our frame layout and all its attributes configured,

53
00:06:43,910 --> 00:06:45,910
I'll edit the xml instead.

54
00:06:48,410 --> 00:06:51,610
Replace frame layout with the new container.

55
00:07:00,910 --> 00:07:04,110
Notice that android studio changes the closing tag as well.

56
00:07:04,710 --> 00:07:06,810
Changing the fragment tag is easier

57
00:07:07,170 --> 00:07:09,170
because a light bulb will do that for us.

58
00:07:09,420 --> 00:07:13,720
In fact, Google have changed the tool tip and we can choose the action from that.

59
00:07:21,520 --> 00:07:23,020
Back in the DesignView,

60
00:07:24,320 --> 00:07:25,620
nothing much changes.

61
00:07:26,020 --> 00:07:30,020
This new fragment container widget was only added in January 2020,

62
00:07:30,420 --> 00:07:34,720
and android studio's layout designer doesn't yet have support for rendering it.

63
00:07:35,420 --> 00:07:38,190
If you're using a later version of android studio,

64
00:07:38,190 --> 00:07:39,790
you may see it rendered differently.

65
00:07:40,990 --> 00:07:43,190
At the moment, there's also a rendering error.

66
00:07:43,990 --> 00:07:47,990
Click the information icon at the top right of the design pane,

67
00:07:48,690 --> 00:07:52,490
and you might see the message failed to instantiate one or more classes.

68
00:07:53,290 --> 00:07:55,650
If you do get that message, you can ignore it.

69
00:07:55,900 --> 00:08:00,400
It's a bug and it'll be fixed in version 1.3.0 of the library.

70
00:08:01,200 --> 00:08:02,700
The app will run fine.

71
00:08:02,700 --> 00:08:07,000
The bug only affects the designer when it tries to render the new widget.

72
00:08:07,770 --> 00:08:10,770
We do have to make a change in our main activity code.

73
00:08:11,130 --> 00:08:14,130
We've got a couple of errors after changing the widget.

74
00:08:16,130 --> 00:08:19,430
The widget works much better than the old fragment container,

75
00:08:19,930 --> 00:08:24,130
and there's now no need to use its view attribute to change visibility.

76
00:08:24,630 --> 00:08:29,090
Delete .view in the three places where we change the visibility.

77
00:08:31,090 --> 00:08:33,450
That's on line 60 in OnCreate.

78
00:08:39,809 --> 00:08:43,610
On line 77, in the ShowEditPane function

79
00:08:47,390 --> 00:08:50,990
and the third place is in the RemoveEditPane function.

80
00:08:56,590 --> 00:08:59,790
Back in the browser find androidx.activity

81
00:09:02,150 --> 00:09:03,710
in the list on the left.

82
00:09:03,710 --> 00:09:06,310
I won't scroll through the document but check the notes

83
00:09:06,610 --> 00:09:08,710
for version 1.0.0.

84
00:09:09,910 --> 00:09:13,570
The activity-ktx module is automatically included

85
00:09:13,570 --> 00:09:15,670
when we use fragment-ktx,

86
00:09:16,370 --> 00:09:18,270
if your app doesn't use fragments,

87
00:09:18,270 --> 00:09:22,260
you can set the dependency to androidx.activity

88
00:09:22,260 --> 00:09:25,560
:activity-ktx instead.

89
00:09:26,360 --> 00:09:29,860
You can see that in the declaring dependencies example

90
00:09:29,860 --> 00:09:31,220
at the top of the page.

91
00:09:31,920 --> 00:09:34,320
All right. We're now up to date,

92
00:09:34,320 --> 00:09:36,570
and we're getting our ViewModel instances

93
00:09:36,570 --> 00:09:38,270
in a more Kotlin-like way.

94
00:09:38,970 --> 00:09:41,330
We've also seen the jetpack documentation.

95
00:09:41,730 --> 00:09:45,060
It's a great resource to check to find out about new things

96
00:09:45,060 --> 00:09:46,860
that Google are doing with the jetpack,

97
00:09:46,860 --> 00:09:49,460
a.k.a androidx libraries.

98
00:09:50,160 --> 00:09:54,360
We're now really ready to start improving the functionality of our app,

99
00:09:54,360 --> 00:09:56,660
and we'll start that in the next video.

100
00:09:57,060 --> 00:09:59,610
Of course, you should run the app and test it

101
00:09:59,610 --> 00:10:02,810
to make sure we haven't broken anything. I'll see you there.

