• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Pass more than one value through intent

Hello,

The below passes in an extra message to a class called DisplayMessageActivity

[HIGH]
/** Called when the user clicks the Send button */
public void sendmessage(View view) {
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); // here extra value
startActivity(intent);

}
[/HIGH]

How can i pass in another value because at the other end i can only get Extra_message which can only be used once....

this is inside the DisplayMessageActivity

[HIGH]

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

[/HIGH]

even if someone can show me how to pass two values i would highly appreciate it.
 
Two strings? I think this one goes back to the 1960s, but you pass

string1 + asc(254) + string2

(where asc(254) is the character with a value such that it can't possibly appear in either string - it can be any character at all).

At the other end, parse for the character and split the string.
 
Im new to android development so please excuse my ignorance, but do you know of a better way to do this? purely because Im not a fan of passing string + string + string and then splitting it.

But if that's the only way to do it then i suppose ill have to crack on with it.
 
You can put as many key-value pairs as you want in an intent (but usually a small number). You can define the key as anything you want. people often use a separate class for this.

the keys: (separate class)
[HIGH]
public class IntentConstants {
public static final String MY_STRING_KEY_1 = "MyStringKey1";
public static final String MY_STRING_KEY_2 = "MyStringKey2";
}[/HIGH]Then, when adding your extras
[HIGH]
intent.putExtra(IntentConstants.MY_STRING_KEY_1, message1);
intent.putExtra(IntentConstants.MY_STRING_KEY_2, message2); [/HIGH]and to retrieve them in the second Activity:
[HIGH]
Intent intent = getIntent(); //get the intent in currently running activity
String message1 = intent.getStringExtra(IntentConstants.MY_STRING_KEY_1);
String message2 = intent.getStringExtra(IntentConstants.MY_STRING_KEY_2);[/HIGH]
 
Back
Top Bottom