Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()?

like image 386
Android_programmer_camera Avatar asked Sep 27 '10 13:09

Android_programmer_camera


People also ask

What is the use of intent CreateChooser () method?

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 .

What is the purpose of an intent in android?

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 ...

What is the purpose of the intent filter?

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.

What is startActivity method in android?

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.


2 Answers

AFAIK, if you use Intent.createChooser, there are three differences:

  1. You can specify the title of the chooser dialog to make it more clear.

  2. The system will always present the chooser dialog even if the user has chosen a default one.

  3. 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

like image 60
Euporie Avatar answered Sep 30 '22 20:09

Euporie


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...

like image 26
WarrenFaith Avatar answered Sep 30 '22 20:09

WarrenFaith