Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between putExtra() and setData()

What is the difference between putExtra() and setData()? I have read the android docs but it is of not much help. Also there is a previous question Intent.setData vs Intent.putExtra but it is still not clear. Thanks in advance.

like image 895
Mrinmoy Sen Avatar asked Oct 03 '13 05:10

Mrinmoy Sen


1 Answers

setData()

Set the data this intent is operating on. This method automatically clears any type that was previously set by setType(String) or setTypeAndNormalize(String).

Note: scheme matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should always write your Uri with a lower case scheme, or use normalizeScheme() or setDataAndNormalize(Uri) to ensure that the scheme is converted to lower case.

Parameters

data: The Uri of the data this intent is now targeting.

Intents are used to signal to the Android system that a certain event has occurred. Intents often describes the action which should be performed and provide data on which such an action should be done. For example your application can start via an intent a browser component for a certain URL. This is demonstrated by the following example.

    String url = "http://www.google.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i); 

But how does the Android system identify the components which can react to a certain intent?

For this the concept of an intent filter is used. An intent filter specifies the types of intents to which that an activity , service, or broadcast receiver can respond to. It declares therefore the capabilities of a component.

Android components register intent filters either statically in the AndroidManifest.xml or in case of a broadcast receiver also dynamically via code. An intent filter is defined by its category, action and data filters. It can also contain additional metadata.

If an intent is send to the Android system, the Android platform runs, using the data included in the Intent object, an receiver determination. In this it determines the components which are registered for the data of the intent. If several components have registered for the same intent filter the user can decide which component should be started.

putExtra()

Add extended data to the intent.

Parameters:

name: The name of the extra data.

value: The String array data value.

Returns the same Intent object, for chaining multiple calls into a single statement.

like image 98
Haresh Chhelana Avatar answered Sep 27 '22 16:09

Haresh Chhelana