Building Intents

Intents are a kind of messaging system used by Android activities to pass information back and forth. It helps keep a separation of concerns between activities.

There are two main types of intents: explicit and implicit.

Explicit intents are best used between activities within your own application, since they require the full class name in order for them to be started.

Here's an example of starting an explicit intent. This would appear in an Activity class:

...
Context context = MainActivity.this;
Class destinationActivity = ChildActivity.class;
Intent intent = new Intent(context, destinationActivity);
startActivity(intent);
...

Implicit intents are good when need a general action to happen, such as opening a webpage, showing a location on a map, or taking a picture. This example shows you how to open a webpage using an implicit intent:

...
Uri webpage = Uri.parse("https://www.taylorsilva.com");
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(intent);
...

When launching implicit intents, you'll want to check that the phone your on has an app that can handle your request. You can wrap your startActivity() in an if() that does that check.

if (intent.resolveActivity(getPackageManager()) != null)
    startActivity(intent);

You should also let the user know there was no app to handle the intent; don't have your app do nothing! The android docs and Udacity course I'm following also recommend disabling whatever feature in your app that launched this implicit intent.

Source: Android Docs

Posted by Taylor