• 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.
 
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]
 
  • Like
Reactions: Atkinson_88
Upvote 0

BEST TECH IN 2023

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.

Smartphones