Why Start Android Share Intent With Chooser?

Jun 17, 2018

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_PERMISSIONstartActivity(Intent.createChooser(intent, "Edit Image"))

vs

val shareIntent = ShareCompat.IntentBuilder        .from(activity)        .setType("text/plain")        .setChooserTitle("Share URL")        .setText(url)        .intentif (shareIntent.resolveActivity(activity.packageManager) != null) {    activity.startActivity(shareIntent)}

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.