Welcome back.
In September 2019,
Google released version 1.0.0
of two libraries that can make working with activities and fragments easier
when using jetpack components.
To use them, we'll add another dependency to our build.gradle file,
the module one not the project one.
The current release of this library is now 1.2.4
but a later version may be available for you.
A good place to learn about these jetpack libraries
and keep track of the version is Google's jetpack pages.
It's worth bookmarking the main jetpack page
then checking regularly to see what's changed.
Jetpack has been continually developed
and new libraries and features are being added all the time.
The note, a short way down the page is important.
We can't compile code that uses this library
if we're compiling to java 7.
We have to specify java 8.
We do that by adding an entry in the android section
of build.gradle.
Make sure you sync now before moving on.
Okay. How does that help us?
It lets us simplify the way we create our view model instances.
I'll change both activities first
because I want to draw your attention to an important difference with fragments.
The change in durations report is the same.
Be careful with your fragments.
If you use exactly the same code in fragment,
it's equivalent to passing this to the ViewModelProvider constructor.
If we want to share the activities view model,
we have to use a slightly different form.
I'll change AddEditFragment first.
Note that we use by ActivityViewModels not ViewModels.
That ensures that we get the instance of the ActivitiesViewModel
equivalent to passing activity on the commented outline above.
I'll make the same change in MainActivityFragment.
While we're dealing with this library,
switch back to the document we were just looking at.
Scroll down to the version 1.2.0 section
and have a look at the top important changes since 1.1.0.
Google have added a FragmentContainerView
which is now the strongly recommended container
for hosting our fragments.
If they strongly recommend it and we're still at the stage of testing and developing our app.
It's a good idea to use one.
Back in android studio
edit the content_mainlayout
in res/layouts.
At the moment, this new container doesn't appear in the palette.
I'm sure Google will add it in there at some point.
But as we've already got our frame layout and all its attributes configured,
I'll edit the xml instead.
Replace frame layout with the new container.
Notice that android studio changes the closing tag as well.
Changing the fragment tag is easier
because a light bulb will do that for us.
In fact, Google have changed the tool tip and we can choose the action from that.
Back in the DesignView,
nothing much changes.
This new fragment container widget was only added in January 2020,
and android studio's layout designer doesn't yet have support for rendering it.
If you're using a later version of android studio,
you may see it rendered differently.
At the moment, there's also a rendering error.
Click the information icon at the top right of the design pane,
and you might see the message failed to instantiate one or more classes.
If you do get that message, you can ignore it.
It's a bug and it'll be fixed in version 1.3.0 of the library.
The app will run fine.
The bug only affects the designer when it tries to render the new widget.
We do have to make a change in our main activity code.
We've got a couple of errors after changing the widget.
The widget works much better than the old fragment container,
and there's now no need to use its view attribute to change visibility.
Delete .view in the three places where we change the visibility.
That's on line 60 in OnCreate.
On line 77, in the ShowEditPane function
and the third place is in the RemoveEditPane function.
Back in the browser find androidx.activity
in the list on the left.
I won't scroll through the document but check the notes
for version 1.0.0.
The activity-ktx module is automatically included
when we use fragment-ktx,
if your app doesn't use fragments,
you can set the dependency to androidx.activity
:activity-ktx instead.
You can see that in the declaring dependencies example
at the top of the page.
All right. We're now up to date,
and we're getting our ViewModel instances
in a more Kotlin-like way.
We've also seen the jetpack documentation.
It's a great resource to check to find out about new things
that Google are doing with the jetpack,
a.k.a androidx libraries.
We're now really ready to start improving the functionality of our app,
and we'll start that in the next video.
Of course, you should run the app and test it
to make sure we haven't broken anything. I'll see you there.