So I'm making a fitness/health app and I need to make some user inputs public such that I can use their values on other screens(activities). I have looked online on how to do this and the closest I got was a companion object. But this is not working as Kotlin is not allowing me to use findViewByID inside a variable inside the class. All the other solutions I have found are for Java and I'm using Kotlin.
I tried making the user inputs a companion object inside the activity's class, but this didn't work after I used findViewByID to grab the user input and put it inside the variable to begin with.
Here is my code, hopefully it's not too long:
package com.example.optilife
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import com.google.firebase.database.DatabaseReference
class LogIn1stTimeActivity : AppCompatActivity() {
companion object {
val userHeight = findViewById<EditText>(R.id.et_UserHeight) //so this doesn't work because it doesn't allow me to use findViewByID
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_log_in1st_time)
val welcomeButton = findViewById<Button>(R.id.welcomeButton)
val userHeight = findViewById<EditText>(R.id.et_UserHeight)
val userWeight = findViewById<EditText>(R.id.et_UserWeight)
val userAge = findViewById<EditText>(R.id.et_UserAge)
welcomeButton.setOnClickListener { var recommendedCalories = 66 + 13.7*userWeight.text.toString().toInt() + 5*userHeight.text.toString().toInt() - 6.8*userAge.text.toString().toInt()
println("$recommendedCalories")
val intent = Intent(this, WelcomeActivity::class.java)
startActivity(intent)}
}
}
So I want to calculate recommendedCalories on another screen and I need the user inputs on that screen. Any help would be appreciated, thanks.
You can just use the intent.putExtra with intent and pass the "recommendedCalories" data.
Get values in WelcomeActivity.class and store them in one variable.