60.8 Launching SecondActivity as a Sub-Activity
In order for SecondActivity to be able to return data to MainActivity, SecondActivity must be started as a sub-activity of MainActivity. This means that the call to startActivity() in the MainActivity returnText() method needs to be replaced with a call to startActivityForResult(). Unlike the startActivity() method, which takes only the intent object as an argument, startActivityForResult() requires that a request code also be passed through. The request code can be any number value and is used to identify which sub-activity is associated with which set of return data. For the purposes of this example, a request code of 5 will be used, giving us a modified MainActivity class that reads as follows:
.
.
class MainActivity : AppCompatActivity() {
private val request_code = 5
.
.
fun sendText(view: View) {
...