This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about Intents and putExtra()

 
Rhys Llewellyn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. So I've been following the Head First Android book and have gotten to Intents. I guess the easiest thing to do is write out the code and then explain where my confusion lies, so here goes:


// So the messageText variable is holding the user input as a String, I get that part. Here's the Receiving class:



Any help would be great.

Thank you

 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rhys, a warm welcome to CodeRanch!

In future, please enclose your code snippets in code tags (see http://www.coderanch.com/how-to/java/UseCodeTags) to make them easily readable.
I've done it for you this time.

Since I don't know how comfortable you are with java and android programming, I'll explain from scratch.
If you already know some of these things, ignore those explanations.

To start off, think of an Intent as a 2-column spreadsheet where each row has a label and a value. Here's an example:

Each row is identified by its label instead of row number.
So rather than asking for value of row 1, we ask for value of row 'name' using Intent.getStringExtra("name").
Or rather than setting the value of row 1, we set the value of row 'name' to 'Rhys' using Intent.putExtra("name", "Rhys").

In your code,

Intent.EXTRA_TEXT is a string constant whose value is "android.intent.extra.TEXT" (see http://developer.android.com/reference/android/content/Intent.html#EXTRA_TEXT)
and messageText is a variable whose value is the text entered in edit control. So if the text entered is "Hello World", the intent will look like this:

On the receiving side, you have

where EXTRA_MESSAGE is a string constant with value "message". The code means look for a row with label "message" and copy its row value into variable messageText.
As you can see in the above spreadsheet, there is no row with label "message". So messageText will be null.
The correct code should be


Alternatively, use ReceiveMessageActivity.EXTRA_MESSAGE in both classes like this:

EXTRA_MESSAGE is a constant whose value remains "message" and that never changes. You can verify by adding a System.out.println(EXTRA_MESSAGE) after the getStringExtra().
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can verify by adding a System.out.println(EXTRA_MESSAGE) after the getStringExtra().

Oops! I forgot this is Android. System.out.println won't help.
Instead use Log.i(EXTRA_MESSAGE) if you're familiar with android logging, or Toast.makeText().show() to show its value on the screen.
 
Rhys Llewellyn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for taking the time and helping me out, Karthik. You've gone above and beyond the kind of help I was expecting, and you explained it very well, I understand it!

I've often Googled Java related posts and been pointed to this forum and have noticed it is just so much friendlier and not so elitist, like other forums. I like it here.

Thanks again
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! Like you, I like it too here at CodeRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic