Results 1 to 2 of 2
  1. #1
    alex067 is offline Member
    Join Date
    Sep 2012
    Rep Power

    Default [Android] Understanding This Activity!

    Hi everyone, posting it on here because it has the most traffic.

    I am learning the basics of Android Development, and I'm studying how activities send values to another activity. My book gives me the following code:
    Java Code:
     public void onSendMessage(View view)
            EditText messageView=(EditText)findViewById(;
            String DisplayText = messageView.getText().toString();
            Intent intent = new Intent(this, ReceievMessage.class);
            intent.putExtra(ReceievMessage.EXTRA_MESSAGE, DisplayText );
    All this function is doing is passing a string to another activity / layout to display that message.

    I understand that the "intent.putExtra(ReceiveMessage.EXTRA_MESSAGE, DisplayText); is storing the string "DisplayText" to the value of "ReceievMessage.EXTRA_MESSAGE" and passing it on.

    Here is where I am confused.

    Java Code:
    public class ReceievMessage extends Activity {
        public static final String EXTRA_MESSAGE = "";
        protected void onCreate(Bundle savedInstanceState) {
            Intent intent = getIntent();
            String messageText = intent.getStringExtra(EXTRA_MESSAGE);
            TextView messageView = (TextView)findViewById(;
    This is the activity that is supposed to simply display that string. I don't understand why I need to do "String messageText = intent.getStringExtra(EXTRA_MESSAGE);" when I already have "public static final String EXTRA_MESSAGE = "";" Isn't "EXTRA_MESSAGE" supposed to already contain the string passed on by the previous activity?

    Also, what is the point of "TextView messageView = (TextView)findViewById(;" Why do I need to find the id of the message, when I am passing the message along from the previous activity? Isn't it redundant to pass the string along, and also find the id of the string?


  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: [Android] Understanding This Activity!

    why I need to do "String messageText = intent.getStringExtra(EXTRA_MESSAGE);"
    Intents can hold data to be passed in a map-like structure (uses key-value pairs). The sending activity uses a put method with key-value to add data to the intent
    and the receiver uses a get method (in above quote) with that same key to retrieve the data.
    One problem I see with the code is that the key (EXTRA_MESSAGE) is an empty String. It'd be better if it had a value, say "MyMessage".

    what is the point of "TextView messageView = (TextView)findViewById(;"
    The variable: is created by the Android pre-compiler program which scans the XML files and creates labels for the components referenced in the XML.
    Look at the XML to find where message is defined. The findViewById method uses the value passed to it to get a reference to the TextView component where the String: messageText is to be displayed.

    For more information read the API doc for the classes and methods.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Android Activity Lifecycle
    By hriday in forum Forum Lobby
    Replies: 0
    Last Post: 01-08-2015, 02:22 PM
  2. Replies: 1
    Last Post: 12-22-2014, 06:12 PM
  3. Replies: 2
    Last Post: 11-28-2013, 06:35 PM
  4. Help Please Override Activity with Android
    By francojava1 in forum Android
    Replies: 2
    Last Post: 10-12-2010, 06:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts