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()
?
The system will always present the chooser dialog even if the user has chosen a default one. If your intent created by Intent. createChooser doesn't match any activity, the system will still present a dialog with the specified title and an error message No application can perform this action .
An Intent object carries information that the Android system uses to determine which component to start (such as the exact component name or component category that should receive the intent), plus information that the recipient component uses in order to properly perform the action (such as the action to take and the ...
An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.
To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
AFAIK, if you use Intent.createChooser
, there are three differences:
You can specify the title of the chooser dialog to make it more clear.
The system will always present the chooser dialog even if the user has chosen a default one.
If your intent created by Intent.createChooser
doesn't match any activity, the system will still present a dialog with the specified title and an error message No application can perform this action
. Or for the normal intent, you may get an Android runtime error with: Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent
The chooser enables the user to pick another mail application than the default. Its very useful if you use normal gmail (privat) and email (work related) and you want to choose which one to take.
Should always be used...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With