Additional options for how the Activity should be started
The name of the extra data
The value of the extra data
the code of the request, to identify it on onActivityResult method
Any action to perform via this intent, ex: Intent.ACTION_VIEW
data uri to be used by intent to perform specified action
Context to use to initialize the Intent
Class to be used by this intent
Caveats of using implicit intent
When calling a implicit intent it's always helpful to check if it's possible by the system to handle it.
This can be done by checking using PackageManager.queryIntentActivities(Intent intent, int flags)
Starting Activity which is a singleTask or singleTop
When the activity's launch mode is singleTask or singleTop, the onActivityResult will be called as soon as the activity is started with a data null. To prevent this, use Intent.setFlags(0) to reset the default flags.
Broadcasting Messages to Other Components
Intents can be used to broadcast messages to other components of your application (such as a running background service) or to the entire Android system.
To send a broadcast within your application, use the LocalBroadcastManager class:
To send a broadcast to components outside of your application, use the sendBroadcast() method on a Context object.
Sometimes you may want to start a new activity while removing previous activities from the back stack, so the back button doesn't take you back to them. One example of this might be starting an app on the Login activity, taking you through to the Main activity of your application, but on logging out you want to be directed back to Login without a chance to go back. In a case like that you can set the FLAG_ACTIVITY_CLEAR_TOP flag for the intent, meaning if the activity being launched is already running in the current task (LoginActivity), then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
It's also possible to use the flags FLAG_ACTIVITY_NEW_TASK along with FLAG_ACTIVITY_CLEAR_TASK if you want to clear all Activities on the back stack:
This is a good alternative to using a WebView for some cases. It allows loading of a web page with an Intent, with the added ability to inject some degree of the look and feel of your app into the browser.
Here is an example of how to open a url using CustomTabsIntent
To use custom tabs, you need to add this dependency to your build.gradle
Receiving the result can be done using the Fragment's method onActivityResult(). You need to make sure that the Fragment's parent Activity also overrides onActivityResult() and calls it's super implementation.
In the following example ActivityOne contains FragmentOne, which will start ActivityTwo and expect a result from it.
In this example MainActivity will start a DetailActivity and then expect a result from it. Each request type should have its own int request code, so that in the overriddenonActivityResult(int requestCode, int resultCode, Intent data) method in MainActivity , it can be determined which request to process by comparing values of requestCode and REQUEST_CODE_EXAMPLE (though in this example, there is only one).
A few things you need to be aware of:
Data is only returned once you call finish(). You need to call setResult() before calling finish(), otherwise, no result will be returned.
Make sure your Activity is not using android:launchMode="singleTask", or it will cause the Activity to run in a separate task and therefore you will not receive a result from it. If your Activity uses singleTask as launch mode, it will call onActivityResult() immediately with a result code of Activity.RESULT_CANCELED.
Be careful when using android:launchMode="singleInstance". On devices before Lollipop (Android 5.0, API Level 21), Activities will not return a result.
You can use explicit or implicit intents when you call startActivityForResult(). When starting one of your own activities to receive a result, you should use an explicit intent to ensure that you receive the expected result.
An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters.
This example shows, how to start intent from browser:
This intent will start app with package com.sample.test or will open google play with this package.
In activity this host and path can be obtained from intent data:
Intent URI syntax:
Open a URL in a browser
Opening with the default browser
This example shows how you can open a URL programmatically in the built-in web browser rather than within your application. This allows your app to open up a webpage without the need to include the INTERNET permission in your manifest file.
In some cases, the URL may start with "www". If that is the case you will get this exception:
android.content.ActivityNotFoundException : No Activity found to handle Intent
The URL must always start with "http://" or "https://". Your code should therefore check for it, as shown in the following code snippet:
Check if there are no apps on the device that can receive the implicit intent. Otherwise, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.
Open Google map with specified latitude, longitude
You can pass latitude, longitude from your app to Google map using Intent
Passing custom object between activities
It is also possible to pass your custom object to other activities using the Bundle class.
There are two ways:
Serializable interface—for Java and Android
Parcelable interface—memory efficient, only for Android (recommended)
Parcelable processing is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.
Sending Activity Code
Receiving the object in destination activity.
You can pass Arraylist of Parceble object as below
Note: There are Android Studio plugins such as this one available to generate Parcelable code
Sending Activity Code
Receiving the object in destination activity.
Arraylist of Serializable object: same as single object passing
This example illustrates sending a String with value as "Some data!" from OriginActivity to DestinationActivity.
NOTE: This is the most straightforward way of sending data between two activities. See the example on using the starter pattern for a more robust implementation.
It is also possible to pass other primitive data types as well as arrays, Bundle and Parcelable data. Passing Serializable is also possible, but should be avoided as it is more than three times slower than Parcelable.
Serializable is a standard Java interface. You simply mark a class as Serializable by implementing the Serializableinterface and Java will automatically serialize it during required situations.
Parcelable is an Android specific interface which can be implemented on custom data types (i.e. your own objects / POJO objects ), it allows your object to be flattened and reconstruct itself without the destination needing to do anything. There is a documentation example of making an object parcelable.
Once you have a parcelable object you can send it like a primitive type, with an intent object:
Or in a bundle / as an argument for a fragment:
and then also read it from the intent at the destination using getParcelableExtra:
Or when reading in a fragment from a bundle:
Once you have a Serializable object you can put it in an intent object:
and then also read it from the intent object at the destination as shown below:
Passing different data through Intent in Activity
1. Passing integer data:
2. Passing double data:
3. Passing String data:
4. Passing ArrayList data :
5. Passing Object data :
Note : Keep in mind your custom Class must implement the Serializable
6. Passing HashMap<String, String> data :
HashMap<String, String> hashMap;
7. Passing Bitmap data :
This will pre-fill an email in a mail app of the user's choice.
If you need to add an attachment, you can use Intent.ACTION_SEND instead of Intent.ACTION_SENDTO. For multiple attachments you can use ACTION_SEND_MULTIPLE
A word of caution: not every device has a provider for ACTION_SENDTO, and calling startActivity() without checking with resolveActivity() first may throw an ActivityNotFoundException.
Share simple information with differents apps.
Share an image with differents apps.
Sharing Multiple Files through Intent
The String List passed as a parameter to the share() method contains the paths of all the files you want to share.
It basically loops through the paths, adds them to Uri, and starts the Activity which can accept Files of this type.
Showing a File Chooser and Reading the Result
Starting a File Chooser Activity
Reading the Result
Start an activity
This example will start DestinationActivity from OriginActivity.
Here, the Intent constructor takes two parameters:
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)
Another way to create the Intent to open DestinationActivity is to use the default constructor for the Intent, and use the setClass() method to tell it which Activity to open:
Start the dialer
This example shows how to open a default dialer (an app that makes regular calls) with a provided telephone number already in place:
Result from running the code above:
Start Unbound Service using an Intent
A Service is a component which runs in the background (on the UI thread) without direct interaction with the user. An unbound Service is just started, and is not bound to the lifecycle of any Activity.
To start a Service you can do as shown in the example below:
You can use any extras from the intent by using an onStartCommand() override:
This pattern is a more strict approach to starting an Activity. Its purpose is to improve code readability, while at the same time decrease code complexity, maintenance costs, and coupling of your components.
The following example implements the starter pattern, which is usually implemented as a static method on the Activity itself. This static method accepts all required parameters, constructs a valid Intent from that data, and then starts the Activity.
An Intent is an object that provides runtime binding between separate components, such as two activities. The Intent represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but here, your intent starts another activity.
This pattern also allows you to force additional data to be passed with the intent.
The ExampleActivity can then be started like this, where context is an activity context:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0