Reputation: 161
I am trying to return a result from a child activity to the parent activity. This is my code in the parent activity
private val REQUEST_CODE = 1
override fun onCreate(savedInstanceState: Bundle?) {
startActivityForResult(Intent(this,DescriptionActivity::class.java).apply{
putExtra("EXTRA_FOOD_NAME", foodName)
putExtra("EXTRA_IMAGE", image)
putExtra("EXTRA_FOOD_DESC", foodDesc)
},REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == REQUEST_CODE &&
resultCode == Activity.RESULT_OK){
//It is null when I check it inside here
val orderedFood = intent.getStringExtra("EXTRA_ORDER")
current_oreder.add(orderedFood)
}
}
Child activity
override fun onCreate(savedInstanceState: Bundle?) {
val foodName = intent.getStringExtra("EXTRA_FOOD_NAME")
btnAddToOrder.setOnClickListener{addToOrder(foodName)}
}
private fun addToOrder(foodName:String?)
{
val orderIntent: Intent = Intent().apply {
putExtra("EXTRA_ORDER", foodName)
}
setResult(Activity.RESULT_OK, orderIntent)
finish()
}
I checked if the child activity's foodName variable has a null value but it has the correct value. It changes to null when I return it to the parent activity. I tried solutions I found on the internet but it didn't work. Can anyone fix this issue?
Upvotes: 1
Views: 190
Reputation: 135
Used in the onActivityResult method:
data?.getStringExtra("EXTRA_ORDER")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == REQUEST_CODE &&
resultCode == Activity.RESULT_OK){
//It is null when I check it inside here
val orderedFood = data?.getStringExtra("EXTRA_ORDER")
current_oreder.add(orderedFood)
}
}
Upvotes: 1