we have to send data between two activities, using onActivityResult
How to send data inbetween the activities
This method is depreacaed but still you can use like code mentioned below.
In Main Activity start activity for result like below snippet.
startActivityForResult(Intent(this@MainActivity,ResultActivity::class.java),123)
And also override on Activity result method in Main Activity like below snippet .
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
123 -> {
if (resultCode == RESULT_OK) {
val returnValue = data?.getStringExtra("some_key")
Toast.makeText(baseContext, returnValue, Toast.LENGTH_SHORT).show()
}
}
}
}
Create a another activity let say ResultActivity and write a function like below snippet which should be called when you want to send back result (i.e any data) .
private fun setTResult() {
val resultIntent = Intent()
resultIntent.putExtra("some_key", "Android course from coding block")
setResult(RESULT_OK, resultIntent)
finish()
}
You can send any data in intent.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.