Kotlin Vocabulary: Delegates part 2
Delegates help you delegate tasks to other objects and provide better code reuse which you can learn more about in this article. Kotlin not only supports an easy way to implement delegates with
by keyword but also provides built-in delegates such as,
notNull(), in the Kotlin Standard Library. Let’s see these built-in delegates and how they work under the hood.
lazy() function is a property delegate that helps you to lazily initialize properties, that is when they are first accessed.
Kotlin Vocabulary: Delegates
One way to get some work done is to delegate that work to another party. No, I am not talking about delegating your work to your friend, but delegating work from one object to another.
Guess what, delegation is not new to software. Delegation is a design pattern in which an object handles a request by delegating to a helper object, called the delegate. The delegate is responsible for handling the request on behalf of the original object and making the results available to the original object.
Kotlin makes delegation easier by providing support for class and property delegates and even containing some built-in delegates of its own. …
Imagine developing an app where you need to deal with time calculations. After some googling, you’ll most likely end up finding great samples with the java.time package! However, after releasing your app, suddenly you get thousands of crashes and complaints from users on API < 26!
Want to know the reason? Watch the video or read the blog below.
The reason is that the devices which encountered the issues simply didn’t ship with the necessary classes, as at the time those classes were not part of Android.
With each new release of Android additional java.* APIs are added from OpenJDK to Android. In Android 11, we added support for a number of APIs from newer OpenJDK releases all the way up to version 13, including additions to List, Set, Map, and the new java.time API. While we continuously add new Java APIs to each platform release, we also want to make these APIs available for older devices. …
The title might sound like a B-rated horror movie, but in reality, Kotlin’s “reified” keyword helps you do things that were not possible before. Generics provide type safety and help you avoid explicit type casts. Generics extend the type system to allow a type or method to operate on objects of various types while providing compile-time type safety. On the other hand, generics can be limiting when you need to access type info in a generic function, and the compiler tells you the info doesn’t exist!
In Java, the
static keyword is used to denote methods and properties that belong to an object, not to an instance of an object. The
static keyword is also used to create Singletons, one of the most widely used design patterns. Singletons help you to create a single instance of an object, which can be accessed and shared by other objects.
Kotlin has a more elegant way to deal with this. You can use a single keyword:
object, to implement the Singleton pattern. Read on to find out the differences between implementing a Singleton in Java vs Kotlin, how you can create Singletons in Kotlin without using the
static keyword (spoiler this is achieved by using the
object keyword), and to find out what’s happening under the hood when you’re using
Creating and managing local databases is a core component of most mobile apps. But whether you’re using SQLite directly or through the Room persistence library, Android developers have been asking for a better way to inspect and debug databases in their running app. To learn more watch the video or read the blog below.
The latest version of Android Studio 4.1 (currently available in Canary) comes with a new tool called Database Inspector, which helps you inspect, query, and modify databases in your running app.
Debugging UI issues can be tricky. Android Studio 4.0 comes with an updated Layout Inspector that lets you debug your Android app UI (user interface) in a way that’s similar to Chrome dev tools. You can read along or if you prefer you can watch the video.
The Layout Inspector works with your device or the Android Emulator and displays the current view hierarchy. This helps pinpoint issues and discover the root causes. Unlike the previous version, the updated Layout Inspector can show the view hierarchy in a 3D perspective which lets you visually see how views are laid out. With this, you can inspect the view hierarchy in layers. …