Can I use the repeated binding data in the variable?

Viewed 28

Please understand my poor English :)

Can I use it as follows?

before

if (binding.heightEditText.text.isNotBlank() && binding.weightEditText.text.isNotBlank()) {
    val intent = Intent(this, ResultActivity::class.java).apply {
        putExtra("weight", binding.weightEditText.text.toString().toFloat())
        putExtra("height", binding.heightEditText.text.toString().toFloat())
    }
    startActivity(intent)
}

after

val height = binding.heightEditText.text
val weight = binding.weightEditText.text

if (height.isNotBlank() && weight.isNotBlank()) {
    val intent = Intent(this, ResultActivity::class.java).apply {
        putExtra("weight", weight.toString().toFloat())
        putExtra("height", height.toString().toFloat())
    }
    startActivity(intent)
}
1 Answers

Yes, you can use this way to define those variables. Just make sure your variables are in the same method that you are calling to retrieve the updated values into defined variables.

I'm updating minor things in your code.

val height = binding.heightEditText.text.toString()
val weight = binding.weightEditText.text.toString()

if (height.isNotBlank() && weight.isNotBlank()) {
    val intent = Intent(this, ResultActivity::class.java).apply {
        putExtra("weight", weight.toFloat())
        putExtra("height", height.toFloat())
    }
    startActivity(intent)
}

I hope it's helpful to you!

Related