Search code examples
androidparameters

Android, Can I use putExtra to pass multiple values


I want to pass two values to another activity can I do this with putExtra or do I have to do it a more complicated way, which it seems from my reading. E.g.. can something like this work?

public final static String ID_EXTRA="com.fnesse.beachguide._ID";

Intent i = new Intent(this, CoastList.class);
i.putExtra(ID_EXTRA, "1", "111");
startActivity(i);

The above gives an error.

Edit

The first thing I tried was similar to:

i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");

but ID_EXTRA2 seems to write over ID_EXTRA1

So,

i.putExtra(ID_EXTRA, new String[] { "1", "111"});

Looks like the go but how do I extract the values from the array in the second activity, I have been using this for a single value.

passedVar = getIntent().getStringExtra(CoastList.ID_EXTRA);

I guess I have to turn ID_EXTRA into an array somehow???


Solution

  • You can pass multiple values by using multiple keys. Instead of

    i.putExtra(ID_EXTRA, "1", "111");
    

    do

    i.putExtra(ID_EXTRA1, "1");
    i.putExtra(ID_EXTRA2, "111");
    

    Of course you have to define 2 constants for the keys and have to read both seperately in the new activity.

    Or you can pass a string array via

    i.putExtra(ID_EXTRA, new String[] { "1", "111"});