From the course: Android Development Essential Training: App Architecture with Kotlin
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Create companion object functions - Android Tutorial
From the course: Android Development Essential Training: App Architecture with Kotlin
Create companion object functions
- [Instructor] So far, my Android app simply presents a set of dice and a string. But I want it to be dynamic. When the user presses a button, I want to roll the dice and see a randomized set of five dice and a string that indicates what kind of result I got. And I should be able to do this over and over again and see different results. So I need some business logic. When implementing business logic that's unrelated to an app's visual presentation, such as getting a set of randomized numbers, it's a good practice to define it in a separate class of some kind, particularly when the logic doesn't really have anything to do with Android and could work on any Java based platform. You can consider creating the logic in a class' companion object. Companion object functions in Kotlin are like static methods in Java. They don't require instantiating their parent classes, and they release any memory or resources when they're complete. I'm going to create this new class in a new package that…
Contents
-
-
-
-
(Locked)
Work with synthetic view bindings5m 10s
-
(Locked)
Get view references with findViewById()4m 53s
-
(Locked)
Handle view events with lambdas5m 26s
-
(Locked)
Create companion object functions6m 16s
-
(Locked)
Update display from new data4m 47s
-
(Locked)
Restore state after orientation change7m 10s
-
(Locked)
Add a ViewModel to an activity4m 43s
-
(Locked)
Subscribe to changes in LiveData values7m 57s
-
(Locked)
-
-
-