The most common use of intent is to bind application components. Intent is used to start, stop, and transmit between activities of an application.
The prompt about starting a new activity in this section also applies to the service. Chapter 2 describes how to start (and create) a service.
To open different screen (activity) in the application, call startactivity and input an intent, as shown in the following snippet:
Startactivity (myintent );
Intent can be opened explicitly by specifying a class or include the action to be executed by the target. In the latter case, the activity will be selected during the runtime to open, using a well-known processing process-"intent Parsing"
The startactivity method is used to find and start a single activity that best matches intent.
When startactivity is used, your application will not receive any notifications when the newly started activity ends. To track the feedback of the Open Screen, use the startactivityforresult method to describe more details later.
Explicitly start a new activity
As you have learned in Chapter 2nd, an application consists of multiple internally linked screens, activity, which must be included in the manifest of the application. To connect them, you may want to explicitly specify which activity to open.
To explicitly select an activity class for startup, you need to create a new intent to specify the context of the current application and the class of the activity to be started. Then pass the intent to startactivity, as shown in the following code snippet:
Intent intent = new intent (myactivity. This, mytheractivity. Class );
Startactivity (intent );
After startactivity is called, the new activity (in this example, mytheractivity) will be created and become visible and active, and moved to the top of the activity stack.
When the code calls the finish method of the new activity, it will be closed and removed from the stack. You can use the back button of the device to navigate to the previous activity.
Implicit intent and runtime binding
Implicit intent is a mechanism that allows anonymous application components to request service actions. When creating a new implicit intent, you specify the action to be executed as an option. You can provide the data required for this action.
When you use this new implicit intent to start the activity, Android will parse it at runtime and find the class that best suits to execute the action on the specified data type. This means that you can create projects that use other applications without having to know exactly which application you will borrow in advance.
For example, if you want a user to make a call in an application, instead of implementing a new dialing, it is better to use an implicit intent to request a call number (represented by a URI) as shown in the following code snippet:
If (somethingweird & itdontlookgood)
{
Intent intent = new intent (intent. action_dial, Uri. parse ("Tel: 555-2368 "));
Startactivity (intent );
}
Android parses this intent and starts an activity that provides the ability to perform a dial-up action on a number. Here, it is a dial-up activity.
Some local applications provide components that execute actions on specific data. Third-party applications, including yours, can also be registered to support new actions or provide an alternative to local actions. You will learn some local actions later in this chapter.