Intents are used as a message-passing mechanism that works both within your application and between applications. You can use Intents to do the following:
- Explicitly start a particular Service or Activity using its class name
- Start an Activity or Service to perform an action with (or on) a particular piece of data
- Broadcast that an event has occurred
You can use Intents to support interaction among any of the application components installed on an Android device, no matter which application they’re a part of. This turns your device from a platform containing a collection of independent components into a single, interconnected system.
One of the most common uses for Intents is to start new Activities, either explicitly (by specifying the class to load) or implicitly (by requesting that an action be performed on a piece of data). In the latter case the action does not need to be performed by an Activity within the calling application.
You can also use Intents to broadcast messages across the system. Applications can register Broadcast Receivers to listen for, and react to, these Broadcast Intents. This enables you to create event-driven applications based on internal, system, or third-party application events.
Android broadcasts Intents to announce system events, such as changes in Internet connectivity or battery charge levels. The native Android applications, such as the Phone Dialer and SMS Manager, simply register components that listen for specific Broadcast Intents — such as “incoming phone call” or “SMS message received” — and react accordingly. As a result, you can replace many of the native applications by registering Broadcast Receivers that listen for the same Intents.
Using Intents to Launch Activities
The most common use of Intents is to bind your application components and communicate between them. Intents are used to start Activities, allowing you to create a workflow of different screens.
To create and display an Activity, call startActivity, passing in an Intent, as follows:
The startActivity method finds and starts the single Activity that best matches your Intent.
You can construct the Intent to explicitly specify the Activity class to open, or to include an action that the target Activity must be able to perform. In the latter case, the run time will choose an Activity dynamically using a process known as intent resolution.
Explicitly starting an Activity
Intent intent = new Intent(MyActivity.this, MyOtherActivity.class);
After startActivity is called, the new Activity (in this example, MyOtherActivity) will be created, started, and resumed — moving to the top of the Activity stack.
Calling finish on the new Activity, or pressing the hardware back button, closes it and removes it from the stack. Alternatively, you can continue to navigate to other Activities by calling startActivity. Note that each time you call startActivity, a new Activity will be added to the stack; pressing back (or calling finish) will remove each of these Activities, in turn.