0

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 ?

user1712778
  • 21
  • 1
  • 2
  • 1
    Do you mean how does Android handle the process in the background or just generally how is it utilized in a general application? – zgc7009 Jun 15 '14 at 19:32
  • Possible duplicate of [how to use putExtra() and getExtra() for string data](http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data) – hungryghost Jun 15 '14 at 19:50
  • My doubt is that how does putExtra() work ? I mean how does it work on the 2 parameters ? And if it stores EXTRA_MESSAGE + message then while extracting why do I just get the message party and not what is written in the EXTRA_MESSAGE ? – user1712778 Jun 16 '14 at 19:56

2 Answers2

3
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.

Community
  • 1
  • 1
Ryhan
  • 1,815
  • 1
  • 18
  • 22
  • Hey Ryan, I got the intent part, I am just confused about what the getExtra() do with it's parameters ? I mean, what happens to EXTRA_MESSAGE and message and if there is some kind of processing, does it get stored at some place or is it like a channel, what goes from putExtra() gets retrieved at getStringExtra() and if that is the case, why only is the "message" part retrieved and not what was written in "EXTRA_MESSAGE" and what does "MainActivity.EXTRA_MESSAGE" stand for ? – user1712778 Jun 16 '14 at 20:00
0

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?