How to start a new activity ? How to pass a value from an activity to the other ? Probably these questions have come to your mind if you have just started using Kotlin for developing android apps . By the end of this post you’ll have all the answers .
We shall use Kotlin-android-extensions to access the widgets here . In our example we’ll start ActivityB from MainActivity, we shall pass a message to ActivityB from MainActivity . Here is our MainActivity.kt
package com.droidmonk.beginkotlin import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val msgToBeSent ="This msg has been sent from MainActivity" btn_load_activity_b.setOnClickListener { val intent = Intent(this, ActivityB::class.java) intent.putExtra(ActivityB.KEY_MSG, msgToBeSent) startActivity(intent); } } }
Let us closely look at the following statement
val intent = Intent(this, ActivityB::class.java)
Q: What is :: ?
:: operator is used to access member references or class references in Kotlin . The Intent() constructor requires an instance of Class type as the second parameter which we get with .class in java .
https://kotlinlang.org/docs/reference/reflection.html
Q: If :: gets us the class reference , why the .java at the end ?
Because :: gives us the the Kotlin class reference which is an instance of KClass , but we need a java class reference (an instance of Class) . KClass has an extension property named java which returns the corresponding java Class instance , thus ActivityB::class.java gives us the java class reference for ActivityB .
Again , the following statement may not look any different from the way it is written in java .
intent.putExtra(ActivityB.KEY_MSG, msgToBeSent)
But there is something new . Though it looks like we are accessing a static constant KEY_MSG from ActivityB (i.e. ActivityB.KEY_MSG) , it is not true . Kotlin does not support static members , thus we are using companion objects which let us access the constants of a class with syntax similar to accessing static constant in java . Now lets have a look at our ActivityB.kt .
package com.droidmonk.beginkotlin import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_b.tv_msg class ActivityB : AppCompatActivity() { companion object { const val KEY_MSG="msg" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_b) val msg=intent.getStringExtra(KEY_MSG) tv_msg.text=msg } }
Well , we have seen how to use companion objects as an alternative to static constants . Now the following question may already have come to your mind , so lets find the answer –
Q. Is a member of a companion object same to a static member ?
No, the documentation says –
“Note that, even though the members of companion objects look like static members in other languages, at runtime those are still instance members of real objects”
I hope this post helps you in your initial steps into Kotlin . Happy coding .