Properties and functions in a Kotlin companion object are available from the class definition. In this video, learn how to encapsulate business logic in 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 …
Looking for study partners?Join the Associate Android Developer Exam study group
The Android app architecture constantly evolves. The introduction of the Kotlin programming language, new form factors such as Chromebooks, and reusable architecture components have made it faster and easier than ever to build high-quality Android apps. It is imperative that developers understand what the latest tools and recommendations are and how to use them to build out their applications. In this course, instructor David Gassner explains how to build an Android app that uses Kotlin instead of Java, add navigation functionality, and leverage the most useful architecture components such as LifecycleObserver, ViewModel, and LiveData, to create apps that are robust and easy to maintain over time.
- Handling lifecycle events
- Handling view events
- Updating display from new data
- Adding ViewModels to activities
- Subscribing to changes in LiveData values
- Adding a new launcher activity
- Starting activities with explicit intents
- Sharing data with implicit intents
- Adding a navigation drawer
- Adding a header to a navigation drawer
- Handling navigating drawer menu events