What is the purpose of using Intent.createChooser() in StartActivity() while sending email in Android

Viewed 52619

When ever we need to send an email in Android we will invoke registered email application using Intent.ACTION_SEND like below

Intent i = new Intent(Intent.ACTION_SEND);
startActivity(Intent.createChooser(i, "Send mail..."));

My doubt is why do we need to use Intent.createChooser in startActivity rather than using startActivty(i). Is there any specific reason of using Intent.createChooser()?

7 Answers

From the document

Android provides two ways for users to share data between apps:

  • The Android Sharesheet is primarily designed for sending content outside your app and/or directly to another user. For example, sharing a URL with a friend.
  • The Android intent resolver is best suited for passing data to the next stage of a well-defined task. For example, opening a PDF from your app and letting users pick their preferred viewer.
val sendIntent: Intent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "Text to send.")
    type = "text/plain"
}


// WILL start Android intent resolver
startActivity(sendIntent)


// WILL start Android ShareSheet
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)

Android Sharesheet have some benefits such as it supports DirectShare, rich preview with Copy option from Android 10 (but some few devices don't have rich preview even Android > 10)

Related