So I have class named Bidding.kt, and I have companion object in that class to be passed to the MainActivity.kt class. Also I have a function that returns as int value.
companion object {
const val bidInputKey = "bidInput"
}
val bidInput = returningBid() //this function returns an int value
val returnIntent = Intent().apply {
putExtra(bidInputKey, bidInput)
}
In the MainActivity.kt class, when I called the bidInput to be assigned to a new variable and passed it into a class's parameter, it required to be converted to Int (I set the class's parameter data type as Int). I don't understand why it changed the data type and detect it as a String, I declare it as Int on Bidding class.
class openBid(var input: Int) {
//doing some stuff here
}
val puttingBid = Bidding.bidInputKey
val addBid = openBid(puttingBid) // I get error in this line because
// puttingBid is detected as a String, not Int
I can avoid the error by adding .toInt() when declaring puttingBid, but I want to know why the data type is changed by itself.
CodePudding user response:
If you're trying to pass that Int
to MainActivity
as an extra on the Intent
, you need to pull that value out of the extras, same as how you put it in:
val puttingBid = intent.getIntExtra(Bidding.bidInputKey, DEFAULT_VALUE_IF_MISSING)
All bidInputKey
is is an identifier string, used to store a value in the Intent
and then retrieve it later. There's nothing magical about it that would make it fetch the Int
itself or anything! You need to do that yourself.