Checking out the decompiled Java code of your Kotlin classes helps you get a better understanding of what’s happening behind the scenes. In this video, see how Kotlin Android Extensions work in more detail.
- [Instructor] Checking out the recompiled Java code of your kotlin classes will help you get a better understanding of what's happening behind the scenes. Lets take a look at the code for our Planet List activity class to see how Kotlin Android extensions work. So we're going to use a quick action and we're going to go to kotlin bi code. And then we're going to click on decompile so that it's easier to follow. So the first thing that you notice as we scroll down is that the planet list activity now has this view cache which is just a hash map.
So whenever we want to find elements of our view this find cached view by ID method is executed. So if we navigate to it, we notice the first thing it tries to do is create the hash map if it's empty. Otherwise it looks into the hash map to get the view and if it can't find it, then it does Androids find view by ID lookup and stores it inside of the hash map. So this is just a really nice experience when working with our code because we know that the find view by ID method will now be executed again and again.
So now lets go back to the planet list activity. If we scroll down to our view holder for our list here on line 64 and 65, we're also making use of the find view by ID method. So lets use Kotlin extensions in order to clean this up as well. Now it's not quite as straightforward as it was in the on create method, but at the time of this recording it's still an experimental feature. So we're going to need to enable it first.
And in order to do that we have to go to our applications build.gradle file. And lets just scroll down to the bottom to make it easier on ourselves and we're going to add and flag for Android Extensions. And we're going to set experimental to true. And now we can sync our project again and this is going to allow us to go back to our planet list activity class and make the following changes to our view holder.
So here in our view holder, we're now, instead of having it simply implement RecyclerView.ViewHOlder, it's going to implement a new class and that's the Layout Container. This is part of the extensions package and is one of the experimental features. But since we're going to implement the layout container then we need to override the container view property. So we can do that by coming here and typing override val and then lets rename view to container view.
And just to make this a little bit easier on ourselves lets put this to a new line and we're going to replace all instances to view with the container view. So one, two, and three. Now that we've done that, we can go ahead and remove the find view by ID calls. So what's really cool about this is that we end up deleting everything inside of the curly braces. And all we're left with is this. Neat huh? So notice that we do have a few errors back inside of our on bind view holder method.
And that's because they were expecting the text view and the image view to have camel case names. But inside of our layout file we were using snake case. So lets go ahead and just change the names to make it easier So the first one was called plane_name and that's from our layout file. And then our image view was called planet_image. So lets run the application again and see that everything behaves just as it did before.
So if we switch over to our emulator we get our list of planets just like we did before and we still have their images. So if by change we were using any extra third party libraries to bypass the find view by decalls, we don't have to use those anymore. Now it's your turn. Switch to the planet detail activity class and update it so that it uses Kotlin Extensions for its view interactions.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections