6 Kotlin Tips for Android Development

Tram Ho

As you all know Kotlin is a strong language. And it has been recognized by google as an official language for Android application development. In the development process, there are many “cool” features that Java does not. So this article is intended to re-list the most commonly used tips and tricks.

LAZY LOADING

We think of lazy loading when the following situations apply:

  • Want to reduce screen startup time.
  • Memory optimization.
  • Encapsulate initialization logic for variables.

Suppose in reality you are making a shopping screen that has the function of buying Item in it. But not on that screen, the user will buy the Item. Always initializing the API reduces screen initialization time and wastes memory. The principle of Lazy loading is to only allocate memory for the variable when it is called and use that value for subsequent calls.

If you declare as above, the purchasingApi variable will not be allocated always when you create the class. It only allocates memory when the variable is first called. Or you can use it to encapsulate initialization logic as follows:

LAMBDAS

LAMBDAS will help us reduce the number of lines of code in source files and enable functional programming. Kotlin already supports this and you don’t have to use third-party libraries like Retrolambda . For example, when you need to listen for a button’s onclick event:

Even returning values ​​from Lambdas is as follows:

DATA CLASSES

Data class is a simple class. Often used as a model class for data. And supported a number of built-in functions: equals (), hashCode (), copy (), and toString ()

Just as above, we have created a data class. In some cases you can get data from the API that needs to be combined with some Json parse libraries like Gson , then you can create default initialization functions and default values ​​for variables as follows:

COLLECTION FILTERING

In the process of developing an application, you often encounter cases where you need to filter a list item based on a certain condition. Here my example is filtering out an active user list from a user list returned from the API.

Kotlin has built a filter function for collection that makes developer work much easier.

EXTENSIONS

EXTENSIONS is useful because it allows you to add a function to a class without inheriting that class. For example, in reality, you would expect Activity to have a function such as: hideKeyboard (). With Kotlin you can do this easily:

With the above writing, you have added a hideKeyboard () function to all Activity classes. Help organize our code much clearer and more readable. Instead of writing the Utility functions as before.

LEVERAGING LET

Let is a sope function. And it allows you to execute a block of code when another object is null. This helps us avoid checking null and code so it is easier to read. In Java it looks like this:

In Kotlin it looks like this:

In the above way, let automatically assigns the user to the variable un-nullable: it. You can use the variable it without having to worry about null checking anymore.

Recently I have listed 6 tips and tricks used in Kotlin. Hope to be of great help to you in the process of developing Android applications. Happy Coding !!! ?

The article is available at: https://savvyapps.com/blog/kotlin-tips-android-development

Share the news now

Source : Viblo