What is intent in Android ?
An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:
Starting an activity:- like startActivity(), startActivityForResult().
Starting a service:- like startService(), bindService().
Delivering a broadcast:- like sendBroadcast(), sendOrderedBroadcast().
Types of Intents
Explicit intents :-
When you explicitly define which Android component should be opened on some user action, then you use explicit intents. You generally use an explicit intent to start a new component in your own app, because you know which exact activity or service you want to start. For example, you can start a new activity in response to a user action or start a service to download a file in the background.
Example :- Intent intent = new Intent(this, MyActivity.class);
Implicit intents do not name a specific component to perform a particular action, but instead it declares a general action to be performed, which allows any component, even from another app to handle it.
Example :- To create an implicit intent you need to add the following component to the intent .
Component name :-
This is optional, but it's the critical piece of information that makes an intent explicit, meaning that the intent should be delivered only to the app component defined by the component name. Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category—described below). If you need to start a specific component in your app, you should specify the component name.
Action :- A string that specifies the generic action to perform (such as view or pick).
Use this action in an intent 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.
Intent intent = new Intent(Intent.ACTION_VIEW);
Also known as the share intent, you should use this in an intent with startActivity() when you have some data that the user can share through another app, such as an email app or social sharing app.
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/html");
To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().
File fileToShare = new File("/sdcard/test.mp3");
Intent i = new Intent(); i.setAction(Intent.ACTION_SEND); i.setData(Uri.fromFile(fileToShare)); startActivity(i);
A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an intent, but most intents do not require a category. Here are some common categories:
The target activity allows itself to be started by a web browser to display data referenced by a link, such as an image or an e-mail message.
The activity is the initial activity of a task and is listed in the system's application launcher.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
This will be in key-value pairs for additional information that should be delivered to the component handling the intent. The extras can be set and read using the putExtras() and getExtras() methods respectively.
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
Flags :- These flags are optional part of Intent object and instruct the Android system how to launch an activity, and how to treat it after it's launched etc.
Intent i=new Intent(this, Sample.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i);