• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused about Intents and putExtra()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rhys, a warm welcome to CodeRanch!

In future, please enclose your code snippets in code tags (see https://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 Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! Like you, I like it too here at CodeRanch
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic