Last active
February 10, 2023 16:31
-
-
Save BanHammerYKT/725797f9074ba6b5e50a9f99b3954660 to your computer and use it in GitHub Desktop.
StartActivityForResult but with parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Example usage: | |
captureImageActivity.launch(IntentWithParam(Intent(MediaStore.ACTION_IMAGE_CAPTURE), SOME_PARAM)) | |
private val captureImageActivity = registerForActivityResult(StartActivityForResultWithParam()) { result -> | |
if (result.resultCode == Activity.RESULT_OK) { | |
val SOME_PARAM = result.param | |
... | |
} | |
} | |
*/ | |
data class IntentWithParam( | |
val intent: Intent, | |
val param: Any?, | |
) | |
data class ActivityResultWithParam ( | |
val resultCode: Int, | |
val data: Intent?, | |
val param: Any?, | |
) | |
class StartActivityForResultWithParam : ActivityResultContract<IntentWithParam, ActivityResultWithParam>() { | |
var param: Any? = null | |
override fun createIntent(context: Context, input: IntentWithParam): Intent { | |
param = input.param | |
return input.intent | |
} | |
override fun parseResult( | |
resultCode: Int, | |
intent: Intent? | |
): ActivityResultWithParam = ActivityResultWithParam(resultCode = resultCode, data = intent, param = param) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment