• 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


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

/** 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


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


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


even if someone can show me how to pass two values i would highly appreciate 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)
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
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:
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]
  • Like
Reactions: Atkinson_88
Upvote 0


We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.