61.9 Returning Data from a Sub-Activity
ActivityB is now launched as a sub-activity of MainActivity, which has, in turn, been modified to handle data returned from ActivityB. All that remains is to modify ActivityB.kt to implement the finish() method and to add code for the answerQuestion() method, which is called when the “Answer Question” button is touched. The finish() method is triggered when an activity exits (for example when the user selects the back button on the device):
fun answerQuestion(view: View) {
finish()
}
override fun finish() {
val data = Intent()
val returnString = editText1.text.toString()
data.putExtra("returnData", returnString)
setResult(RESULT_OK, data)
super.finish()
}
All that the finish() method needs to do is create a new intent...