Reputation: 1
How do I send values that the user puts in editText in StartActivity to textView3 in Second Activity? I am essentially asking the user for his name in StartActivity and then printing "Your name is ___" in the SecondActivity.
I did try to use and then in the SecondActivity but it shows null what do I do?
StartActivity::
button.setOnClickListener
{
val name = editText.text.toString()
val intent2 = Intent (this, SecondActivity::class.java)
intent.putExtra("name", name)
startActivity(intent2)
}
SecondActivity::
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val name2 = intent.getStringExtra("name")
textView3.text = name2
}
The error was that it prints "Your name is null".enter code here
Upvotes: 0
Views: 63
Reputation: 2133
Change intent.putExtra("name", name)
to intent2.putExtra("name", name)
Note: Whenever you don't have a variable intent
in your scope. Kotlin extensions take intent
value from getIntent()
which is your previous intent. And that's why you have missed the compilation error.
Upvotes: 0
Reputation: 54254
This looks like a typo that Kotlin is helping you make. The name of the intent you're starting is intent2
, but you're adding the "name" string to intent
. This is possible because Kotlin is letting you access the results of getIntent()
(i.e. the Intent
object used to launch the current activity) as though it were a property.
Change this line to use intent2
and it should all start working:
intent2.putExtra("name", name)
Upvotes: 7