2

I'm new to android and i have noticed that i can share an intent in 2 ways.

first way :

ShareCompat.IntentBuilder.from(this). setType(mimeType). setChooserTitle(title). setText(text). startChooser();

second way is:

Intent shareIntent =   ShareCompat.IntentBuilder.from(this)
                                                .setChooserTitle(title)
                                                .setType(mimeType)
                                                .setText(text)
                                                .getIntent();
    if (shareIntent.resolveActivity(getPackageManager()) != null){
        startActivity(shareIntent);
    }

my question is ,does using startChooser() saves me from the check that i use in the second way? ..also is there any other differences between these two functions?

Abdel-Rahman
  • 539
  • 5
  • 19

2 Answers2

2

Does using startChooser() saves me from the check that i use in the second?

No.

IntentBuilder is basically a helper class for constructing sharing intents(Intent#ACTION_SEND and Intent#ACTION_SEND_MULTIPLE) and starting activities to share content.

also is there any other differences between these two functions

Under the hood, both startChooser() and startActivity() perform same action. startChooser() just wraps startActivity(). Check out the definition of startChooser():

    /**
     * Start a chooser activity for the current share intent.
     *
     * <p>Note that under most circumstances you should use
     * {@link ShareCompat#configureMenuItem(MenuItem, IntentBuilder)
     *  ShareCompat.configureMenuItem()} to add a Share item to the menu while
     * presenting a detail view of the content to be shared instead
     * of invoking this directly.</p>
     */
     public void startChooser() {
        mActivity.startActivity(createChooserIntent());
     }

I would recommend you to use second approach, which gives you better control with which you can handle the error scenario and present meaningful information to the user.

Sagar
  • 23,903
  • 4
  • 62
  • 62
0

First way :

Here you give your Intent to framework but you do not know if any application is available in device to handle that Intent or not.

second way :

You first make sure that at-least one application is available in device which can handle the Intent.

In second way you have a way to handle unexpected-flow without passing that intent to framework. Like you can notify user about "no application available to handle that action".

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186