• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Confused about Intents and putExtra()

 
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

 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • 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 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
  • 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 Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! Like you, I like it too here at CodeRanch
 
Quick! Before anybody notices! Cover it up with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!