60.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 returnText() method, which is called when the “Return Text” 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 returnText(view: View) {
finish()
}
override fun finish() {
val data = Intent()
val returnString = editText2.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, add the...