startActivityForResult() is deprecated in AndroidX 234 . Its replacement, the Activity Result API, uses an ActivityResultContract<I, O> that turns a typed input into an Intent and the reply into a typed output, and a launcher that runs it. BookNest opens NoteActivity and gets the note back as a String?:
class EditNote : ActivityResultContract<String, String?>() {
override fun createIntent(context: Context, input: String): Intent =
Intent(context, NoteActivity::class.java).putExtra(EXTRA_TITLE, input) // explicit
override fun parseResult(resultCode: Int, intent: Intent?): String? =
if (resultCode == Activity.RESULT_OK) intent?.getStringExtra(EXTRA_NOTE) else null
}
val editNote = rememberLauncherForActivityResult(EditNote()) { note ->
Log.i("BookNest", "note result: $note")
lastNote = note
}NoteActivity's Save calls setResult(RESULT_OK, ...) and finish(). Saving a note, then opening the editor again and pressing Back, logs:
09-24 18:39:14.971 9771 9771 I BookNest: note result: Great API chapter 09-24 18:39:27.449 9771 9771 I BookNest: note result: null
Register on every creation of the screen: after process death the result goes to the callback registered under the same key. Ready-made contracts include PickVisualMedia, TakePicture and RequestPermission.