I would like to know how I can find out which application the user has clicked on in the content sharing dialog. I currently use this method:
private fun showNativeShareDialog(contentUri: Uri) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "image/*"
putExtra(Intent.EXTRA_STREAM, contentUri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val chooserIntent = Intent.createChooser(intent, "Compartir imagen")
shareResultLauncher.launch(chooserIntent)
}
private val shareResultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
val chosenComponent = result.data?.getStringExtra(Intent.EXTRA_CHOSEN_COMPONENT)
chosenComponent?.let { packageName ->
when {
packageName.contains("whatsapp") -> {
// Usuario eligió WhatsApp
}
packageName.contains("instagram") -> {
// Usuario eligió Instagram
}
// etc...
}
}
}
}
The problem I have with this, is that I depend on how the target application is configured, and normally when I share and go back, I get a resultCode of type RESULT_CANCELLED, is there a way to detect when it is pressed before navigating to the target app?