Ceridoglu
Ceridoglu

Reputation: 75

Sending information between Activities in Kotlin

    fun kullaniciOlustur2(view: View){
        val intent = Intent(applicationContext,KullaniciOlustur2::class.java)

        intent.putExtra("input",makeUsername.text.toString())
        intent.putExtra("input2",makeUserphone.text.toString())
        startActivity(intent)
    }

Mainactivity2 starts here..before this, I was using

val intent = intent 
val received: String = intent.getStringExtra("input")   
makeUsername.text = received 

But this method doesn't work anymore. I tried using getIntent() but couldn't get anything

   val intent = getIntent()

Upvotes: 1

Views: 6996

Answers (3)

Rohan Lodhi
Rohan Lodhi

Reputation: 1395

Use Below Code: The Code is same as Java .Just difference is kotlin do not have 1. No semicolon at the end. 2. to call another activity kotlin use ::

ex.KotlinActivity::class.java

startActivity(Intent(this, KotlinActivity::class.java).putExtra("DataTrasfer", ""))

To Get Value:

intent.getStringExtra("DataTrasfer")

Upvotes: 0

TapanHP
TapanHP

Reputation: 6201

Try this code

Activity 1

val intent = Intent(FirstActivity.this,SecondActivity::class.java) //not application context
intent.putExtra("input",makeUsername.text.toString())
intent.putExtra("input2",makeUserphone.text.toString())
startActivity(intent)

Activity 2

inside onCreate() method use

val stringOne = getIntent().getStringExtra("input")

Or more cleaner way is

val extras = getIntent().getExtras()
if (null != extras) {
    val value = extras.getString("input")
    //The key argument here must match that used in the other activity
}

and please check similar answers in Java, you may be able to get the Idea here already told in another answer.

I also Use Anko to remove this kind of boilerplate code

Upvotes: 3

user9679023
user9679023

Reputation:

I recommend using Kotlin Anko, there are plenty of methods that will help you to remove this boilerplate code

check Anko Intents here

Upvotes: 1

Related Questions