Skip to content

Instantly share code, notes, and snippets.

@prateek54
Created May 21, 2022 18:56
Show Gist options
  • Save prateek54/027ca2f5ca00653dfe4dda76640fab4c to your computer and use it in GitHub Desktop.
Save prateek54/027ca2f5ca00653dfe4dda76640fab4c to your computer and use it in GitHub Desktop.
Custom Result Contract Kotlin Class
class CustomContract : ActivityResultContract<String, String?>() {
const val DATA = "data"
const val INPUT_DATA = "input_data"
override fun createIntent(context: Context, input: String?): Intent {
val intent = Intent(context, DummyResultActivity::class.java)
intent.putExtra(INPUT_DATA, input)
return intent
}
override fun parseResult(resultCode: Int, intent: Intent?): String? {
return when (resultCode) {
//Transforming our result to required format before returning it
Activity.RESULT_OK -> intent?.getStringExtra(DATA)
else -> null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment