简体   繁体   中英

Passing several string values into another Activity

I am currently learning Android. I would like to send several string values from one Activity to another one after pressing the button. I can send one value with the usage of Intent but what about 3 different values? Is it possible to make it in the same way or should I put them into a list or something?

Intent a1 = new Intent(SecondActivity.this, MainActivity.class);
Intent a2 = new Intent(SecondActivity.this, MainActivity.class);
Intent b = new Intent(SecondActivity.this, MainActivity.class);

a1.putExtra("KEY1", String.valueOf(value1));
a2.putExtra("KEY2", String.valueOf(value2));
b.putExtra("KEY3", String.valueOf(value3));

Intent add_activity = new Intent(getApplicationContext(), 
MainActivity.class);
startActivity(add_activity);

You don't have to create multiple intents for this. Use only one intent like below

Intent add_activity = new Intent(SecondActivity.this, MainActivity.class);

add_activity.putExtra("KEY1", String.valueOf(value1));
add_activity.putExtra("KEY2", String.valueOf(value2));
add_activity.putExtra("KEY3", String.valueOf(value3));

startActivity(add_activity);

If you want to pass them in a list you can do it like this

ArrayList<String> myList = new ArrayList<String>();
myList.add(String.valueOf(value1));
...
...
add_activity.putExtra("mylist", myList);
startActivity(add_activity);

You can get it in the other activity like below

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

you put your three String in three different references, of course, you must have one value in second activity determined which activity will launch

Intent add_activity = new Intent(getApplicationContext(), MainActivity.class);
            add_activity.putExtra("KEY1", String.valueOf(value1));
            add_activity.putExtra("KEY2", String.valueOf(value2));
            add_activity.putExtra("KEY3", String.valueOf(value3));
            startActivity(add_activity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM