-
Notifications
You must be signed in to change notification settings - Fork 0
Extension Function
fun Context.isNetworkStatusAvailable(): Boolean {
val connectivityManager = this
.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
connectivityManager?.let {
val netInfo = it.activeNetworkInfo
netInfo?.let {
if (netInfo.isConnected) return true
}
}
return false
}
isNetworkStatusAvailable()
is extension functions of Context class. It's like adding functions in Context class as:
abstract class Context{
fun Context.isNetworkStatusAvailable(): Boolean {
val connectivityManager = this
.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
connectivityManager?.let {
val netInfo = it.activeNetworkInfo
netInfo?.let {
if (netInfo.isConnected) return true
}
}
return false
}
}
Extensions functions can be declared by adding prefix of class name as Context.isNetworkStatusAvailable()
.
Suppose you want to add the extension functions in Activity class you can declare functions as Activity.yourFunc()
.
Let's have a closer look at extension function in android development
//without using extension functions
val editor = getSharedPreferences("my_pref", Context.MODE_PRIVATE).edit()
editor.putString("name", "rajesh")
editor.putInt("age", 21)
editor.apply()
//using extension function
sharedPreferences.edit {
putString("name", "rajesh")
putInt("age", 21)
}
// Extension function
fun SharedPreferences.edit(a: SharedPreferences.Editor.() -> Unit) {
val editor = edit()
editor.a()
editor.apply()
}
// Extension property
val ContextWrapper.sharedPreferences get() = getSharedPreferences("my_pref", Context.MODE_PRIVATE)
You can explore some useful extension functions used in the android projects over here.
open class A {
fun add(a: Int, b: Int): Int {
print("member function")
return a + b
}
}
class C : A() {
fun A.add(a: Int, b: Int): Int {
print("extended function")
return a + b
}
}
fun main(args: Array<String>) {
var a = C()
a.add(4, 6)
}
which function will be executed extended or member?
- Priority will be the member functions.
Output: extended function
class MySingleton {
companion object{}
}
fun MySingleton.Companion.subtract() {
print("singleton subtract")
}
uses:
MySingleton.subtract()
Limitation:
To add the extension function in singleton class it must have companion object.
fun main(args: Array<String>) {
var a = A()
print(a.add(2,3))
}
fun A.add(a: Int, b: Int): Int {
sum = a + b
return sum
}
open class A {
var sum = 0
}
Kotlin compiler compiles into bytecode which is executable by the JVM. let's have a look at the internal class of MainKt.class.
public final class MainKt
{
public static final void main(@NotNull final String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
final A a = new A();
System.out.print(add(a, 2, 3));
}
public static final int add(@NotNull final A $receiver, final int a, final int b) {
Intrinsics.checkParameterIsNotNull($receiver, "receiver$0");
$receiver.setSum(a + b);
return $receiver.getSum();
}
}
Under the hood its static class which passes receiver as object in add(@NotNull final A $receiver, final int a, final int b)
functions.
As per Kotlin official documentation
Extensions do not actually modify classes they extend. By defining an extension, you do not insert new members into a class, but merely make new functions callable with the dot-notation on variables of this type.
We would like to emphasize that extension functions are dispatched statically, i.e. they are not virtual by receiver type. This means that the extension function is called is determined by the type of the expression on which the function is invoked, not by the type of the result of evaluating that expression at runtime.
Resources