44

As I'm learning Kotlin for Android development, I'm now trying the basic programs like hello world and how to navigate from one activity to another activity, there is no issue with this.

When I move from one activity to another, it works fine, but I do not know how to pass the values between the activities.

I tried to set the values in one activity and retrieved them in another activity it does not work.

Please see the code snippet below

This is my main activity where I take the username and password from edit text and setting to the intent:

class MainActivity : AppCompatActivity() {
    val userName = null
    val password = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener {
            val intent = Intent(this@MainActivity,SecondActivity::class.java);
            var userName = username.textø
            var password = password_field.text
            intent.putExtra("Username", userName)
            intent.putExtra("Password", password)
            startActivity(intent);
        }
    }
}

This is my second activity where I have to receive values from the main activity

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        var strUser: String = intent.getStringExtra("Username")
        var strPassword: String = intent.getStringExtra("Password")
        user_name.setText("Seelan")
        passwor_print.setText("Seelan")
    }
}

Please guide me on how to do this, whether I have some other way to do this in Kotlin if not by intent.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jeyaseelan
  • 571
  • 1
  • 4
  • 14
  • Kotlin should make it simpler. We should be able to pass data like Parameters in Activity. I am pretty sure it is not hard to achieve. – M. Usman Khan Oct 31 '22 at 09:37

8 Answers8

88

Send value from HomeActivity

val intent = Intent(this@HomeActivity, ProfileActivity::class.java)
intent.putExtra("Username", "John Doe")

startActivity(intent)

Get values in ProfileActivity

val profileName = intent.getStringExtra("Username")
Boken
  • 4,825
  • 10
  • 32
  • 42
Sudip Sadhukhan
  • 1,784
  • 12
  • 21
11

I'm on mobile, you must test by yourself.

Try to make a CharSequence to a String in MainActivity , you have put a CharSequence rather than a String, for example:

var userName = username.text.toString()
var password = password_field.text.toString()
holi-java
  • 29,655
  • 7
  • 72
  • 83
11

In Kotlin, you can pass the data simply by using the Intents. You can directly put your data in intent or you can write those data in bundle and send that bundle to another activity using the intent.

val intent = Intent(this@HomeActivity, ProfileActivity::class.java);
intent.putExtra("profileName", "John Doe")
var b = Bundle()
b.putBoolean("isActive", true)
intent.putExtras(b)
startActivity(intent);
Boken
  • 4,825
  • 10
  • 32
  • 42
AaRiF
  • 2,980
  • 2
  • 14
  • 13
3

You can simply use the intents and bundle to send data from one activity to another activity.

val intent = Intent(this@OneActivity,TwoActivity::class.java);
intent.putExtra("username", userName)
startActivity(intent);
2
    //On Click on Button 

    var but = findViewById<Button>(R.id.buttionActivity_two) as Button

            but.setOnClickListener {
    //Define intent 
                var intent = Intent(applicationContext,MainActivity::class.java)
// Here "first" is key and 123 is value                
intent.putExtra("first",123)
                startActivity(intent)

            }

        }

// If Get In Into Other Activity
var Intent1: Intent
Intent1= getIntent()
//Here first is key and 0 is default value
      var obj :Int  =  Intent1.getIntExtra("first",0);
      Log.d("mytag","VAlue is==>"+obj)
Love kothari
  • 97
  • 1
  • 1
  • 5
2

first you should do this,

var userName = username.text.toString()
var password = password_field.text.toString()

Add Anko dependency.

 implementation "org.jetbrains.anko:anko:0.10.4"

information passing inside MainActivity() is like

startActivity<SecondActivity>("Username" to userName,"Password" to password )

get information from SecondActivity() is,

val profileName=intent.getStringExtra("Username")
Muhammed Haris
  • 340
  • 1
  • 5
  • 15
1

You can just access the value without using extras or intent. Simply use companion object in MainActivity:

    companion object{
        val userName: String = String()
        val password: String = String()
    }

In SecondActivity:

    var strUser: String = MainActivity.username
    var strPassword: String = MainActivity.password

And you can access the values from multiple activities easily.

0
Send data  


    val Name=findViewById<EditText>(R.id.editTextTextPersonName)  
    val Name2=findViewById<EditText>(R.id.editTextTextPersonName2)  
    val name=Name.text.toString()  
    val age=Name2.text.toString()  
    val intent1=Intent(this,Second::class.java).also {  
    it.putExtra("Username",name)  
    it.putExtra("Age",age)  
    startActivity(it);   
    }

Receive data

val name=intent.getStringExtra ("Username")
val age = intent.getStringExtra("Age")
val textView5=findViewById<TextView>(R.id.textView).apply {
text= "Name = $name"
}
val textView6=findViewById<TextView>(R.id.textView2).apply {
text= "Age = $age"
}