Kotlin is a developer-friendly language, and it makes the most challenging job(development) in the world a fun job, no matter in which platform you’re working. With Kotlin, you can create your style of code.
If you haven’t read the first part, I highly recommend you read it. The first article contains a detailed guide on how you use powerful & useful features from Kotlin like extensions, higher-order, and scoped functions.
Advanced Android Programming With Kotlin
Kotlin-exclusive features you should be using
medium.com
Having said that, what are you going to learn in this article?
Let’s say we can’t fit the hidden yet powerful features of Kotlin in one or two articles.
Let’s start learning
Kotlin Delegates
A delegate is just a class that provides the value for a property and handles its changes. This allows us to move, or delegate, the getter-setter logic from the property itself to a separate class, letting us reuse this logic.
Let’s see a simple example
Simple delegate example
If you’ve any doubts with syntax, check out in Kotlin Docs here.
What happens here is that we can assign a value to the variable str_sample
through as shown above, but the advantage of writing this is that you don’t need to write .trim() every time.
You can create extensions or custom classes to apply this even more quickly. Read part1 to know how to create extensions?
Isn’t that cool, you might wonder something like it’s sweet but not super useful.
You’re mistaken, it’s super useful, let’s see how
Do you guys remember how we write shared-preference put and get functions, let me jog your memory with a sample
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q00.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q01.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q02.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q03.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q04.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q05.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q06.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q07.html
https://leeuwinestate.com.au/alex/e-1/New-v-Che-li-Q08.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q00.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q01.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q02.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q03.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q04.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q05.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q06.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q07.html
https://aopa.com.au/alex/e-1/New-v-Che-li-Q08.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q00.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q01.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q02.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q03.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q04.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q05.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q06.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q07.html
https://www.cojob.fr/alex/e-1/New-v-Che-li-Q08.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q00.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q01.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q02.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q03.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q04.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q05.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q06.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q07.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/New-v-Che-li-Q08.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-01.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-02.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-03.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-04.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-05.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-06.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-07.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-08.html
https://leeuwinestate.com.au/alex/e-1/Chel-v-Newc-liv-epl-09.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-01.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-02.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-03.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-04.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-05.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-06.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-07.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-08.html
https://aopa.com.au/alex/e-1/Chel-v-Newc-liv-epl-09.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-01.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-02.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-03.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-04.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-05.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-06.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-07.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-08.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/Chel-v-Newc-liv-epl-09.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-01.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-02.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-03.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-04.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-05.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-06.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-07.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-08.html
https://www.cojob.fr/alex/e-1/Chel-v-Newc-liv-epl-09.html
https://carepropertyinvest.be/careon/i-1/crotone-v-lazio-diretta.html
https://carepropertyinvest.be/careon/i-1/crotone-v-lazio-diretta1.html
https://carepropertyinvest.be/careon/i-1/crotone-v-lazio-diretta2.html
https://carepropertyinvest.be/careon/i-1/crotone-v-lazio-diretta3.html
https://carepropertyinvest.be/careon/i-1/crotone-v-lazio-diretta4.html
https://leeuwinestate.com.au/alex/e-1/c-v-ep0000.html
https://leeuwinestate.com.au/alex/e-1/c-v-ep0001.html
https://leeuwinestate.com.au/alex/e-1/c-v-ep0002.html
https://leeuwinestate.com.au/alex/e-1/c-v-ep0003.html
https://leeuwinestate.com.au/alex/e-1/c-v-ep0004.html
https://aopa.com.au/alex/e-1/c-v-ep0000.html
https://aopa.com.au/alex/e-1/c-v-ep0001.html
https://aopa.com.au/alex/e-1/c-v-ep0002.html
https://aopa.com.au/alex/e-1/c-v-ep0003.html
https://aopa.com.au/alex/e-1/c-v-ep0004.html
https://www.cojob.fr/alex/e-1/c-v-ep0000.html
https://www.cojob.fr/alex/e-1/c-v-ep0001.html
https://www.cojob.fr/alex/e-1/c-v-ep0002.html
https://www.cojob.fr/alex/e-1/c-v-ep0003.html
https://www.cojob.fr/alex/e-1/c-v-ep0004.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/c-v-ep0000.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/c-v-ep0001.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/c-v-ep0002.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/c-v-ep0003.html
https://www.uvirtual.edu.co/wordpress/alex/e-1/c-v-ep0004.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-01.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-02.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-03.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-04.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-05.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-06.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-07.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-08.html
https://carepropertyinvest.be/careon/e-1/Chel-v-Newc-liv-epl-09.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-01.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-02.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-03.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-04.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-05.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-06.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-07.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-08.html
https://www.ausy.fr/alena/e-1/Chel-v-Newc-liv-epl-09.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q00.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q01.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q02.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q03.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q04.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q05.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q06.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q07.html
https://www.ausy.fr/alena/e-1/New-v-Che-li-Q08.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q00.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q01.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q02.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q03.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q04.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q05.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q06.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q07.html
https://carepropertyinvest.be/careon/e-1/New-v-Che-li-Q08.html
https://carepropertyinvest.be/careon/e-1/c-v-ep0000.html
https://carepropertyinvest.be/careon/e-1/c-v-ep0001.html
https://carepropertyinvest.be/careon/e-1/c-v-ep0002.html
https://carepropertyinvest.be/careon/e-1/c-v-ep0003.html
https://carepropertyinvest.be/careon/e-1/c-v-ep0004.html
https://www.ausy.fr/alena/e-1/c-v-ep0000.html
https://www.ausy.fr/alena/e-1/c-v-ep0001.html
https://www.ausy.fr/alena/e-1/c-v-ep0002.html
https://www.ausy.fr/alena/e-1/c-v-ep0003.html
https://www.ausy.fr/alena/e-1/c-v-ep0004.html
preference without delegates
Can we reduce the code and make it look smart. Of course, we can,
preference with delegates
Now, let’s see how we can use these delegates with other components. If you observe what we are doing here carefully, you can see that we’re saving a value to a variable and retrieving the value later when required.
We can leverage this functionality and implement saving & retrieving values from the bundle in fragments way more easily.
Let’s see how we save & retrieve values from the bundle in fragments.
Bundle operations without delegates
What we did here is that we save the values into the bundle then assign that bundle to the fragment while creating and later retrieving the values from the bundle in OnCreate
function.
You see, it’s too much work to do; lets make it a bit easy with extensions and delegates.
First, let’s create an extension to insert values in the bundle so that we don’t need to invoke different functions based on datatype. Have a look
Kotlin extension to insert values in Bundles
Then we need to create a class that extends ReadWriteProperty
and override set and get functions as shown below to save and retrieve real-time values. Have a look
Custom Delegate class to automate bundle functionality in fragments
Now create a function to simplify the process of using above class and also easy to understand for new developers in your project.
fun <T : Any> argument(): ReadWriteProperty<Fragment, T>
= FragmentArgumentDelegate()
Now its time to use this delegate and see how it makes our lives easy.
Handling bundle values with Kotlin delegates
That’s all you don’t even need to write any code to put or get values from bundle. Now isn’t that awesome.
Default & Named parameters in functions
Kotlin has many useful and underrated features, I’m sure this is one of them.
Let’s say we’ve to write a function that calculates the sum of two values. The tricky part here is that the user can pass both values or any one of the values or nothing. I’m sure java can handle this as shown below.
Now, let’s see how we can handle this in Kotlin.
fun add(a : Int = 0, b : Int = 0) = a + b
Isn’t this fantastic — a function with parameters having default values. You can pass only a or b value based on your requirements.
Now, how can you pass only a or b values when the function has two parameters. How will the function know which value you’re passing?
This is another useful feature in Kotlin; we can pass a parameters using their names in the actual function. Have a look
Inline functions to use higher-order functions effectively
In Kotlin, the higher-order functions and lambads are stored as objects, so its a memory consumption, and virtual calls might introduce runtime overhead. Sometimes we can eliminate the memory overhead by inlining the code of the higher order functions.
An inline function is declared with a keyword inline. The use of inline function enhances the performance of the higher-order function. The inline function tells the compiler to copy parameters and functions to the call site.
Let’s say the following print is an inline function.
public inline fun print(message: String) {
System.out.print(message)
}
Now let’s invoke the print function from another function have a look.
fun normalFunction(){
print("Hello")
print("world")
}
This is how it appears before compilation, when we declare a function as inline compiler treats it differently. While compiling, it replaces the function call with actual statements in that function. Have a look at normalFunction body after compilation.
fun normalFunction(){
System.out.print("Hello")
System.out.print("world")
}
So that no objects are saved in the memory, and it executes the statements step by step as the normal function does, which leads to better performance.
The mighty when
statement in Kotlin
The primary focus of when
statement in Kotlin is to provide functionality similar to switch
statement. Whereas switch
functionality is restricted to certain limitations like to compare the value and execute different blocks of code.
But when
has a plethora of useful functionalities like it can return the value or execute code blocks based on the values.
Simple ‘when’ block
Let’s see how to execute a normal when
block
when(number) {
0 -> println("apple")
1, 2 -> println("banana")
3 -> println("grape")
4 -> println("Orange")
else -> println("Not a fruite")
}
when
replaces default
keyword in switch
to else
but the functionality is similar. In my opinion else
fit into the syntax than default
.
Return values with ‘when’ block
Now, let’s see how to return a value using when
statement
when with return value
This kind of functionality is not possible in java.
Arbitrary conditions
when
not only supports simple constant comparisons, it also supports inner conditions like range of numbers as shown below
when with arbitrary conditions
Using ‘when’ without argument
The beauty of when
is that we can use it even without an argument. In this case it acts as a simple if/else
block but the advantage here is that it provides a way cleaner and easy to understand interface.
when without argument
Update
To learn more about Kotlin, read rest of the articles in Advanced Programming With Kotlin series:
- “Advanced Programming With Kotlin”
- “Advanced Programming With Kotlin — Part 3”
- “Advanced Programming With Kotlin — Part 4”
- “Advanced Programming With Kotlin — Part 5”
To more learn about Kotlin coroutines and other advanced features, read the following articles: