Skip to content

Instantly share code, notes, and snippets.

@Eldhopj
Last active March 6, 2022 05:48
Show Gist options
  • Save Eldhopj/bb055680b39efa54c54e1d911eb09da9 to your computer and use it in GitHub Desktop.
Save Eldhopj/bb055680b39efa54c54e1d911eb09da9 to your computer and use it in GitHub Desktop.
ActivityResult : Suppose ActivityResult & B are activities the navigation is from A -> B We need the result back, A <- B
// calling the Activity B
resultLauncher.launch(Intent(requireContext(), B::class.java))
// we get data in here from B
private var resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
when (result.resultCode) {
Activity.RESULT_OK -> {
result.data?.getStringExtra("VALUE")?.let {
// data received here
}
}
Activity.RESULT_CANCELED -> {
// cancel or failure
}
}
}
// Sending result value back to A
if (success) {
setResult(RESULT_OK, Intent().putExtra("VALUE", value))
} else {
setResult(RESULT_CANCELED)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment