Hi,
I’m learning both kotlin and android with android tutorials on android developer site. (https://developer.android.com/training/basics/firstapp/26)
I tried writing ‘build your first app’ in kotlin. but I could not refer main activity’s properties from another activity.
here are my codes.
package systems.cyberage.myfirstapp
// package com.example.my.mynewapp
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
// import kotlinx.android.synthetic.res.layout.activity_main.editText
class DisplayMessageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_display_message)
val intent: Intent = getIntent()
val message: String = intent.getStringExtra(MainActivity.EXTRA_MESSAGE) // this does not work: Unresolved reference
val message: String = "com.example.mynewapp.MESSAGE" // this works
textView.text = message
}
}
And My MainActivity.kt is as follow
package systems.cyberage.myfirstapp
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
// import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import kotlinx.android.synthetic.main.activity_main.editText
class MainActivity : AppCompatActivity() {
companion object {
const val EXTRA_MESSAGE : String = “MyFirstApp.MESSAGE”
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(view: View) {
val intent = Intent(this@MainActivity, DisplayMessageActivity::class.java)
// val message:String = intent.getStringExtra(MainActivity().EXTRA_MESSAGE)
val message = editText.text.toString()
intent.putExtra(EXTRA_MESSAGE, message)
startActivity(intent)
}
}
Can anyone help me out on this. Just couldn’t get it going.
Many Thanks,
John