A Puppy has a name, a breed and a whole lot of cuteness. To model a class that holds just data, you should use a
data class. The compiler simplifies your work by auto generating
hashCode() for you and providing destructuring and copy functionality out of the box letting you focus on the data you need to represent. Read on to learn more about other advantages of data classes, their restrictions and to take a look under the hood of how they’re implemented.
To declare a data class, use the data modifier and specify the properties of the class as val or var parameters in the constructor. As with any function or constructor, you can also provide default arguments, you can directly access and modify properties and define functions inside your class. …
Ever since we announced our Kotlin support in 2017 we’ve been getting a lot of questions about Kotlin on Android: you wanted to know whether it’s time to learn it, to introduce it to your app, what the best course or tutorial to learn Kotlin is, whether Google is using Kotlin internally and what our plans for the Java programming language are. In this post, I want to answer some of these questions.
The questions we get most often are on the same line:
Users expect to have a seamless experience with your app. Crashes can lead to an increase in poor reviews, uninstalls and even damaging your brand perception. From talking to the community we know that one of the main reasons to adopt Kotlin is safer code. In this post I’ll share a couple of the ways Kotlin improved the stability of a few of our partners’ code but we’ll also look at the results of some Google Play store stats and see if there’s a correlation between using Kotlin and the number of crashes (spoilers: there is!).
The quality of your app doesn’t only have an impact on your user experience. There are several other elements that will be affected by a high number of…
Sometimes you just want to break down one object that contains multiple fields to initialize several separate variables. To achieve this, you can use Kotlin’s destructuring declarations. Read on to find out how to use this feature, what Kotlin types offer it by default, how to implement it in your own classes or for classes that you don’t control but you think would benefit from destructuring and how everything works under the hood.
Destructuring declarations allows us to define local values/variables as such:
It’s a convenient way to work with data coming from functions or collections:
By default, all data classes support destructuring. …
Short and easy to use, default arguments allow you to implement function overloads without the boilerplate. Like many Kotlin features, this can feel like magic. Are you curious to learn its secrets? Read on to find out how default arguments work under the hood.
If you need to overload a function, instead of implementing the same function multiple times, you can use default arguments:
Default arguments can be applied to constructors as well:
By default, Java doesn’t recognize the default value overload:
To instruct the compiler to generate the overload methods, use the
@JvmOverloads annotation on your Kotlin…
Android Jetpack is a suite of libraries aimed at helping you write high-quality apps easily, supporting older versions of the Android OS. Now, two years after the launch of Jetpack, we’ve seen tremendous adoption by apps, from large developer teams to those just getting started. And that’s just the beginning: today, we’re launching new libraries, along with major updates to existing ones, that we’ve been working on over the past year. Here’s a round up of the latest updates in Jetpack — an extended version of our What’s new in Jetpack talk!
Hilt is a new Android library which simplifies dependency injection (DI) in your application. Hilt lets you focus on just the important parts of defining and injecting bindings without worrying about managing all of the DI setup and wiring. …
You might have had the problem where a
RecyclerView loses the scroll position when your
Fragment is re-created. This usually happens because the
Adapter data is loaded asynchronously and data hasn’t loaded by the time
RecyclerView needs to layout so it fails to restore the scroll position.
RecyclerView offers a new API to let the
Adapter block layout restoration until it is ready. Read on to learn how to use this new API and how it works.
There are several ways to ensure a correct scroll position that you might have adopted. The best one is making sure that you always set the data on the
Adapter before the first layout pass by caching the data you want to display in memory, in a
ViewModel or in a repository. If this approach wasn’t possible, other solutions were either more complicated, like avoiding setting the
Adapter on the
RecyclerView, which can bring issues with items like headers, or misusing
ConcatAdapter is a new class available in
recyclerview:1.2.0-alpha02 which enables you to sequentially combine multiple
adapters to be displayed in a single
RecyclerView. This enables you to better encapsulate your adapters rather than having to combine many data sources into a single adapter, keeping them focused and re-usable.
One use case for this is displaying a list loading state in a header or footer: when the list is retrieving data from the network, we want to show a progress spinner; in case of error, we want to show the error and a retry button.
ConcatAdapter allows us to display the contents of multiple adapters, in a sequence. For example, let’s say that we have the following 3…
You know all of those Util files you create with all sorts of small functions that you end up using a lot throughout your app? If your utility functions get other functions as parameters, chances are you can improve the performance of your app by saving some extra object allocations, that you might not even know you’re making, with one keyword:
inline. Let’s see what happens when you pass these short functions around, what inline does under the hood and what you should be aware of when working with inline functions.
Let’s say that you use
SharedPreferences a lot in your app so you create this utility function to reduce the boilerplate every time you write something in your…
In development, as in life, we know it’s important to avoid doing more work than needed as it can waste memory and energy. This principle applies to coroutines as well. You need to make sure that you control the life of the coroutine and cancel it when it’s no longer needed — this is what structured concurrency represents. Read on to find out the ins and outs of coroutine cancellation.
If you prefer to see a video on this check out the talk Manuel Vivo and I gave at KotlinConf’19 on coroutines cancellation and exceptions:
⚠️ In order to follow the rest of the article without any problems, reading and understanding Part I of the series is required. …