Why Start Android Share Intent With Chooser?

January 31, 2020

Why use Intent.createChooser?

  • You can specify chooser title
  • Will always show chooser dialog even when a default app is selected
  • If no app is available to handle the intent, if will show No application can perform this action message (instead of ActivityNotFoundException exception).
ShareCompat.IntentBuilder
        .from(activity)
        .setType("text/plain")
        .setChooserTitle("Share URL")
        .setText(url)
        .startChooser()

or convert intent into chooser

val intent = Intent(Intent.ACTION_EDIT)
intent.setDataAndType(uri, "image/*")
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
startActivity(Intent.createChooser(intent, "Edit Image"))

vs

val shareIntent = ShareCompat.IntentBuilder
        .from(activity)
        .setType("text/plain")
        .setChooserTitle("Share URL")
        .setText(url)
        .intent

if (shareIntent.resolveActivity(activity.packageManager) != null) {
    activity.startActivity(shareIntent)
}

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.