0
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("plain/text");
sendIntent.setData(Uri.parse("test@gmail.com"));
sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@gmail.com" });
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "test");
sendIntent.putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(sendIntent);

In the code above (from this answer by Jared Burrows) I am confused why we have to specify the action: ACTION_VIEW in the sendIntent definition? We already specified that the activity that we want to start using the sendIntent intent is the "com.google.android.gm", "com.google.android.gm.ComposeActivityGmail" activity.

I thought the only purpose of the action ACTION_VIEW would be if you are making an implicit intent and you want the system to display all of the applications that the user downloaded that have an Activity that can perform the action ACTION_VIEW?

But in this case we already specified which activity we want to start which is ComposeActivityGmail so why put the ACTION_CLOSE action since the system would already which activity to start for this intent.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • Never mind that specific class `ComposeActivityGmail` may use the action in the Activity to perform something different even though the same activity may be used for different actions. –  Feb 22 '17 at 06:16

1 Answers1

0

You can use following code to start another activity:

Intent intent = new Intent(this, Classname.class);
startService(intent);

You use ACTION_VIEW with startActivity() when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app.

In your case, you are passing the email address so that is why you have to use it.

Saarang Tiwari
  • 291
  • 2
  • 12
  • The `ComposeActivityGmail` is used for `ACTION_VIEW` as well as `ACTION_SEND`. So when the `ComposeActivityGmail` receives an intent, in the code that handles the handle in the `ComposeActivityGmail` does it use the Action in the intent to perform some specific behavior? Like for `ACTION_VIEW` it will only show the user something. `ACTION_SEND` it will actually send the message. –  Feb 22 '17 at 17:50
  • 1
    Yes, the action in the intent performs specific behavior. `Intent.getAction()` in the second activity( here `ComposeActivityGmail` ) retrieves the general action to be performed and it does get influenced by `ACTION_VIEW` or `ACTION_SEND`. – Saarang Tiwari Feb 22 '17 at 20:01