JenairoB
JenairoB

Reputation: 3

How can I share a variable across multiple activities? (Kotlin)

I'm trying to share a variable across multiple activities, and all I've been able to find out until now is how to move it from one activity to the next activity (by clicking on a button you go to the next activity and the variable gets carried over). But when trying to repeat this process to one screen further, I keep getting the default value of 0 printed out. Why is this happening?

The code below shows the corresponding code inside of the first of three activities. So here when I click on the button to go to the next activity all these variables get carried over to the next activity. But when I repeat this code on the next activity it doesn't work.

   val welcomeButton = findViewById<Button>(R.id.welcomeButton)
            welcomeButton.setOnClickListener {
                val userHeight = findViewById<EditText>(R.id.et_UserHeight).text.toString().toInt()
                val userWeight = findViewById<EditText>(R.id.et_UserWeight).text.toString().toInt()
                val userAge = findViewById<EditText>(R.id.et_UserAge).text.toString().toInt()
                val userGender = findViewById<Spinner>(R.id.genderSpinner).toString()
                val userActivityLevel = findViewById<Spinner>(R.id.activityLevelSpinner).toString()
                val userWeightGoal = findViewById<Spinner>(R.id.weightGoalSpinner).toString()
                val intent = Intent(this, WelcomeActivity::class.java)
                intent.putExtra("EXTRA_AGE", userAge)
                intent.putExtra("EXTRA_HEIGHT", userHeight)
                intent.putExtra("EXTRA_WEIGHT", userWeight)
                intent.putExtra("EXTRA_GENDER", userGender)
                intent.putExtra("EXTRA_ACTIVITYLEVEL", userActivityLevel)
                intent.putExtra("EXTRA_WEIGHTGOAL", userWeightGoal)
                startActivity(intent)
            }

The code below is the second activity where I am trying to send the variables over to a third activity. But I am unable to use the variables on the third activity. Any thoughts on why?

    val userAge = intent.getIntExtra("EXTRA_AGE", 0)
            val userHeight = intent.getIntExtra("EXTRA_HEIGHT", 0)
            val userWeight = intent.getIntExtra("EXTRA_WEIGHT", 0)
            val userGender = intent.getStringExtra("EXTRA_GENDER")
            val userActivityLevel = intent.getStringExtra("EXTRA_ACTIVITYLEVEL")
            var userWeightGoal = intent.getStringExtra("EXTRA_WEIGHTGOAL")


            val welcomeToHomeButton = findViewById<Button>(R.id.SendToHomeButton)
            welcomeToHomeButton.setOnClickListener {
                intent.putExtra("EXTRA_AGE", userAge)
                intent.putExtra("EXTRA_HEIGHT", userHeight)
                intent.putExtra("EXTRA_WEIGHT", userWeight)
                val intent = Intent(this, HomeActivity::class.java)
                startActivity(intent)
            }

Upvotes: 0

Views: 538

Answers (1)

mol
mol

Reputation: 2657

Every activity has a getIntent method (translated to just intent in kotlin), that returns the intent, that started that Activity.

In your code below, you first set the extras to the intent of the second activity, and then create a new intent that starts the third activity:

welcomeToHomeButton.setOnClickListener {
   intent.putExtra("EXTRA_AGE", userAge)
   intent.putExtra("EXTRA_HEIGHT", userHeight)
   intent.putExtra("EXTRA_WEIGHT", userWeight)
   val intent = Intent(this, HomeActivity::class.java)
   startActivity(intent)
}

You need to create a new Intent first, and then set extras to it, like this:

welcomeToHomeButton.setOnClickListener {
   val intent = Intent(this, HomeActivity::class.java)
   intent.putExtra("EXTRA_AGE", userAge)
   intent.putExtra("EXTRA_HEIGHT", userHeight)
   intent.putExtra("EXTRA_WEIGHT", userWeight)
   startActivity(intent)
}

Upvotes: 0

Related Questions