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 ofActivityNotFoundException
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: