user1712778
user1712778

Reputation: 21

How does putExtra() work ? How does it's parameters work ? And what do the two parameters mean?

I have been trying my hand on Android development and was following on with the guide given at http://developer.android.com/, I came across putExtra() and was wondering if someone could explain to me as to what does this function do ? How does the following code work ?

For sending :

Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);

For receiving:

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

how are the 2 functions, getStringExtra and putExtra, working in this code ?

Upvotes: 0

Views: 4805

Answers (2)

Thiyraash David
Thiyraash David

Reputation: 41

If not mistaken its just key-value pair https://searchenterprisedesktop.techtarget.com/definition/key-value-pair .It is just indication this id(key) is 2(value). From the another activity you can get the value finding by the key(id) ,i.e

Activity B Intent intent = getIntent();

String id = intent.getStringExtra("id");

REFER TO How do I get extra data from intent on Android?

Upvotes: 0

Ryhan
Ryhan

Reputation: 1885

Intent intent = new Intent(this, DisplayMessageActivity.class); // sets target activity
EditText editText = (EditText) findViewById(R.id.edit_message); // finds edit text 
String message = editText.getText().toString(); // pull edit text content
intent.putExtra(EXTRA_MESSAGE, message); // shoves it in intent object

Lets say this = Activity A, and your DisplayMessageActivity = Activity B

In this scenario, your getting the edit text content from Activity A and your communicating its content over to Activity B using the Intent object. Activity B, being interested in the value must pull it out of the intent object, so it would do the following usually in its onCreate() :

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

This is generally the happy, but common case.

However, if Activity B had existed in the backstack, and Activity A wanted to clear the backstack to reach Activity B again, Activity B would have a new intent delivered in its onNewIntent(Intent theNewIntent) method, which you would have to override in Activity B to see this new intent. Or else you would be stuck dealing with the original intent Activity B had first received.

UPDATED

Sounds like your interested in the internals of intents as well as how you get the "EXTRA_MESSAGE" part of the intent. Intents store key-value pairs, so if you want to get the key part, something like the following would work:

for (String key : bundle.keySet()) {
    Object value = bundle.get(key);
    Log.d(TAG, String.format("%s %s (%s)", key,  
        value.toString(), value.getClass().getName()));
}

A quick overview of the internals is that Intents use Android's IPC (Inter-process communication). Essentially, the only data types that are OS-friendly are primitive types (int, long, float, boolean, etc...), this is why putExtra() allows you to store primitives only. However, putExtra() also tolerates parcelables, and any object defining itself as a Parcelable basically defines how the Java object trickles down to its primitives, allowing the intent to deal with those friendly data types once more, so no magic there. This matters because Intents act as wrappers for the Binder layer. Binder layer being the underlying structure of an Intent object, and this implementation lives in the native layer of Android (the c/c++ parts). Effectively, the native layer handles the marshalling/unmarshalling back up to the java layer, where your Activity B gets the data.

I realize this simplification might be skipping too many details, so reference this pdf for better understanding.

Upvotes: 3

Related Questions