Skip to main content

Android Intents

Hello everyone, Today We Will Learn About Intents in Android.



What Are Intents
Android application components can connect to other Android applications. This connection is based on a task description represented by an Intent object.
Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications. For example, an activity can start an external activity for taking a picture.

In Android the reuse of other application components is a concept known as task. An application can access other Android components to achieve a task. For example, from a component of your application you can trigger another component in the Android system, which manages photos, even if this component is not part of your application. In this component you select a photo and return to your application to use the selected photo.

Starting Activities
To start an activity, use the method startActivity(intent). This method is defined on the Context object which Activity extends.

The following code demonstrates how you can start another activity via an intent:-

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

Activities which are started by other Android activities are called sub-activities. This wording makes it easier to describe which activity is meant.


Explicit And Implicit
Android supports explicit and implicit intents. An application can define the target component directly in the intent (explicit intent) or ask the Android system to evaluate registered components based on the intent data(implicit intents).


Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier. Explicit intents are typically used within an application as the classes in an application are controlled by the application developer. The following shows how to create an explicit intent and send it to the Android system to start an activity.

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");

Implicit intents specify the action which should be performed and optionally data which provides content for the action. If an implicit intent is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type. If only one component is found, Android starts this component directly. If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the intent.
For example, the following tells the Android system to view a webpage. All installed web browsers should be registered to the corresponding intent data via an intent filter.

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.blogger.com"));
startActivity(i);


We Have Covered The Part 1 Of Android Intents and  We Will Learn More About Intents In Upcoming Posts, So Stay Tuned.

Install Now Our Andy:Android Tutorials App For Well Explained Chapters, 40+ Examples,Interactive Quizes and Developer Certificate.
:-
Andy:Android Tutorial


Comments