Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the values from activity to another activity

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.

like image 705
Jeyaseelan Avatar asked Jul 18 '17 04:07

Jeyaseelan


People also ask

How do you pass value from one activity to another activity?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How pass data from activity to another activity in Android?

putExtra() method is used for send the data, data in key-value pair key is variable name and value can be Int, String, Float etc. getStringExtra() method is for getting the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra()


1 Answers

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") 
like image 87
Sudip Sadhukhan Avatar answered Sep 28 '22 05:09

Sudip Sadhukhan