Summary of some practical tips developed by kotlin

preface

With the convening of Google I / O conference, Google announced that it would support kotlin as the development language of Android. Recently, articles and introductions about kotlin have been very active.

This article mainly introduces some practical tips about kotlin development, which can be shared for your reference and learning. I won't say much below. Let's take a look at the detailed introduction together.

1. Lazy loading

Delayed loading has several benefits. Delaying loading makes the program start-up time faster because loading is delayed until variables are accessed. This is particularly useful in Android applications that use kotlin rather than server applications. For Android applications, we naturally want to reduce the application startup time so that users can see the application content faster rather than waiting for the initial load screen.

Lazy loading is also more efficient memory, because we only need to call resources to load resources into memory. For example:

If the user never calls gankapi, it will never be loaded. Therefore, it will not occupy the required resources.

Of course, lazy loading can also be better used for encapsulation initialization:

If you don't worry about multithreading or want to improve performance, you can also use

2. Customize getters / setters

Kotlin will automatically use the getter / setter model, but in some cases (such as Jason), we need to use custom getters and setters. For example:

3. Lambdas

4. Data classes

The data class is a simple version of class, which automatically adds methods including equals (), hashcode (), copy (), and toString (). Separate data from business logic.

If you use gson to parse JSON's data class, you can use the default constructor:

5. Collection filtering

6. Object expressions

Object expressions allow you to define singletons. For example:

Threadutil can directly call static class methods:

In a similar way, we create objects instead of anonymous inner classes:

Both are basically the same thing - create a class as a single instance of the declared object.

7. Companion object

Kotlin has no static variables and methods. Correspondingly, you can use companion objects. Associated objects allow you to define constants and methods, similar to static in Java. With it, you can follow the fragment pattern of newinstance.

Familiar usage:

8. Global constants

Kotlin allows global constants across the entire application. In general, a constant should have as little scope as possible, but this is a good way when it is needed globally.

9. Optional parameters

Optional parameters make method calls more flexible without passing null or default values. For example, when defining an animation:

10. Extensions (extension properties)

For example: calling keyboard hiding in activity

Recommend a website that collects extensions. kotlinextensions.com

11. lateinit

Null checking is one of the characteristics of kotlin, so data is initialized when data is defined. However, in Android, some properties need to be initialized in the oncreate () method.

For basic data type:

If using butterknife:

12. Safe typecasting

Secure type conversion is required in Android. When you first perform type conversion in kotlin, you can do this:

But in fact, this can only lead to collapse. When "as" is called, it will perform object conversion, but if the converted object is "null", an error will be reported. The correct use should be "as":

13. Let operator

Let operator: this method is allowed to be executed if the value of the object is not empty.

14. isNullOrEmpty | isNullOrBlank

We need to validate multiple times when developing Android applications. If you don't use kotlin to deal with this problem, you may have found the textutils class in Android.

If the names are all spaces, textutils.isempty is not enough. Isnullorblank is available.

15. Avoid abstract methods of kotlin class

Also use Lambdas as much as possible. This enables more concise and intuitive code. For example, click listening in Java is:

Use in Java:

In kotlin:

If the single abstract method is used in kotlin:

Here's another way:

16. With function

With is a very useful function, which is included in kotlin's standard library. It takes an object and an extension function as its parameters, and then causes the object to extend the function. This means that all the code we write in parentheses is an extension function of the object (the first parameter). We can use all its public methods and properties as this. When we do many operations on the same object, this is very helpful to simplify the code.

17. Static Layout Import

One of the most commonly used codes in Android is to use findviewbyid() to get the corresponding view.

Some solutions, such as the butterknife library, can save a lot of code, but kotlin takes another step by allowing you to import all references to views from an imported layout.

For example, this XML layout:

In activity:

18. Implement POJO class with kotlin

In Java

In kotlin, it can be simplified to:

19. Reduce the use of asynctash

Use with anko lib. The switching between background and main threads is particularly intuitive and simple. Uithread runs on the main thread, and we don't need to care about the activity life cycle (pause and stop), so there will be no errors.

20. Apply function

It looks very similar to with, but it is a little different. Apply can avoid creating a builder, because the function called by the object can initialize itself according to its own needs, and then the apply function will return the same object:

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>