Android KTX User Guide

Tram Ho

Android KTX is a set of Kotlin extensions that are included with Android Jetpack and other Android libraries. The KTX extension provides a concise, straightforward Kotlin for Jetpack, the Android platform, and other APIs.

To do so, these extensions take advantage of some of the features of the Kotlin language, including the following:

  • Extensive functionality
  • Extended properties
  • Lambdas
  • The parameters are named
  • Default value of the parameter
  • Coroutines

For example, when working with SharedPreferences, you must create an editor before you can make optional data modifications. You must also apply or commit those changes when editing is complete, as shown in the following example:

Kotlin lambdas is a perfect fit for this use case. They allow you to take a more concise approach by passing a block of code to execute after the editor is created, allowing code to execute, and then allowing the SharedPreferences API to apply the changes.

Here is an example of one of the KTX Core Android functions, SharedPreferences.edit, which adds an editing function for SharedPreferences.

This function takes the optional boolean flag as its first parameter to indicate whether changes should be committed or applied. It also receives an action to be executed on the SharedPreferences editor as lambda.

The caller can choose to commit or apply changes. The lambda action itself is an anonymous extension function on SharedPreferences.Editor returns the Unit, as indicated by its signature. This is why inside the code block you can do the job directly on SharedPreferences.Editor.

Finally, SharedPreferences.edit () contains the inline keyword. This tag tells the Kotlin compiler that it should copy and paste the compiled bytecode for the function every time the function is used. This avoids wasting resources creating a new class for every action each time the function is called.

Using Android KTX in Project

To use Android KTX in Project, add the following dependency in /build.gradle :

AndroidX Modules

The Android KTX is organized into modules, in which each module contains one or more packages.

You must add dependencies for each module in your app’s build.gradle. Remember to add the appropriate version number. You can find the most recent version number in the respective section for each artifact in this topic.

Android KTX contains a single core module that provides Kotlin extensions for common APIs and several dedicated extensions.

Except for the core module, all the KTX module artifacts replace the basic Java dependency in your build.gradle file. For example, you can replace androidx.fragment: fragment with androidx.fragment: fragment-ktx . This syntax helps to better manage versioning and does not add additional dependency declaration requirements.

Core KTX

The Core KTX module provides extensions to the popular libraries that are part of the Android Framework framework. These libraries do not have the Java dependencies that you need to add to build.gradle.

To add this module, add the following to your app’s build.gradle:

Below is a list of packages included in the Core KTX module:

androidx.core.animation androidx.core.content androidx.core.content.res androidx.core.database androidx.core.database.sqlite androidx.core.graphics androidx.core.graphics.drawable androidx.core.location androidx.core. net androidx.core.os androidx.core.text androidx.core.transition androidx.core.util androidx.core.view androidx.core.widget

Collection KTX

Collection extensions contain utility functions for working with Android’s memory-saving Collection libraries, including ArrayMap, LongParseArray, LruCache, and others.

To use this module, add the following to your app’s build.gradle:

The Collection Extension takes advantage of Kotlin’s operator overloading to simplify things like Collection concatenation, as illustrated in the following example:

Fragment KTX

The Fragment KTX module provides several extensions to simplify the Fragment API.

To use this module, add the following to your app’s build.gradle:

With the Fragment KTX module, you can simplify fragment transaction with lambdas, for example:

You can also bind to ViewModel in one line, using the viewModels and activityViewModels properties:

Lifecycle KTX

Lifecycle KTX defines a LifecycleScope for each Lifecycle object. Any registration processes launched in this range will be destroyed when the Lifecycle is destroyed.

You can access CoroutineScope of the Lifecycle by using the lifeecycle.coroutineScope or lifeecycleOwner.lifecycleScope property.

To use this module, add the following to your app’s build.gradle:

The following example demonstrates how to use lifecycleOwner.lifecycleScope to create a pre-computed asynchronous TextView:

LiveData KTX

When using LiveData, you may need to calculate values ​​asynchronously.

For example, you might want to retrieve a user’s preferences and make them available to your UI.

For these cases, LiveData KTX provides the liveData builder function, which calls suspend function and provides the result as a LiveData object.

To use this module, add the following to your app’s build.gradle:

In the following example, loadUser () is a suspend function declared elsewhere. You can use the liveData builder function to call loadUser () asynchronously, then use emit () to output the result:

Conclude

Thus, in this article, I have instructed you to use Android KTX, which is a collection of Kotlin extensions. Android KTX will make your code concise, easy to understand and use.

Share the news now

Source : Viblo