From the course: Intermediate Kotlin for Android Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Create an extension function

Create an extension function

From the course: Intermediate Kotlin for Android Developers

Start my 1-month free trial

Create an extension function

- We're here inside our solar system sample app. And when you click on one of the planets, it takes you to this detailed view, where you get a description and other information about the planet, which includes its composition. Let's see how this information is being provided right now, and how we can take advantage of an extension function in order to simplify the code. So, we'll come over to Android Studio, and we're here inside the PlanetDetailFragment. This is the class that's responsible for displaying that screen. If we scroll down to the onResume method, we see here on line 62 some very simple logic for determining a planet's composition. If the planet is an inner planet, then it's terrestrial. Otherwise, it's a gas giant. Notice how this depends completely on the inner property of the planet class. So, it makes sense to move this logic there, and we can do that by means of an extension function. So, let's highlight this information here on line 62, and copy it, and then we'll…

Contents