56

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???

7 Answers7

165

You could pass a 'bundle' of extras rather than individual extras if you like, for example:

Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_USERNAME","my_username");
extras.putString("EXTRA_PASSWORD","my_password");
intent.putExtras(extras);
startActivity(intent);

Then in your Activity that your triggering, you can reference these like so:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("EXTRA_USERNAME");
String password_string = extras.getString("EXTRA_PASSWORD");

Or (if you prefer):

Bundle extras = getIntent().getExtras();
String username_string = extras.getString("EXTRA_USERNAME");
String password_string = extras.getString("EXTRA_PASSWORD");
starball
  • 20,030
  • 7
  • 43
  • 238
Steve
  • 1,651
  • 2
  • 10
  • 2
32

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"});
  • 4
    @MikeClarke Make sure that ID_EXTRA1 and ID_EXTRA2 don't have the same values assigned to them *(e.g. both have `com.fnesse.beachguide._ID`)*. Otherwise they count as the same key, which would trigger the overwrite. And if you try to use the array, use `getIntent().getStringArrayExtra()` instead of `getIntent().getStringExtra()`. –  Dec 09 '11 at 23:35
  • Can you please tell how many strings we can pass at a time from one activity to another? There should be a limit to what we can intent, so that the app does not get crashed?? – Rajnish Sharma Oct 13 '20 at 10:17
11

Putting extra values in class

public class MainActivity extends Activity {
        public final static String USERNAME = "com.example.myfirstapp.MESSAGE";
        public final static String EMAIL = "com.example.myfirstapp.EMAIL";

public void registerUser(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText userNameTxt = (EditText) findViewById(R.id.editText1);
        EditText emailTxt = (EditText) findViewById(R.id.editText2);
        String userName = userNameTxt.getText().toString();
        String email = emailTxt.getText().toString();
        intent.putExtra(USERNAME, userName);
        intent.putExtra(EMAIL,email);
        startActivity(intent);

    }

Reading extra values from another class

public class DisplayMessageActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String user = intent.getStringExtra(MainActivity.USERNAME);
        String email = intent.getStringExtra(MainActivity.EMAIL);   
Abhi
  • 6,471
  • 6
  • 40
  • 57
3

Passing bundle of extra for kotlin coders

val intent = Intent(this, MyActivity::class.java)
val extras = Bundle().apply {
    putString("EXTRA_USERNAME", "my_username")
    putString("EXTRA_PASSWORD", "my_password")
}
intent.putExtras(extras)
startActivity(intent)

Then retrieve your values in the other activity like below:

val extras = getIntent().extras
val username_string = extras?.getString("EXTRA_USERNAME")
val password_string = extras?.getString("EXTRA_PASSWORD")
Dharman
  • 30,962
  • 25
  • 85
  • 135
developerTobi
  • 41
  • 1
  • 8
1

No you can't but you can pass an array using:

public Intent putExtra (String name, String[] value)

like this for example:

i.putExtra(ID_EXTRA, new String[]{"1", "111"});
gwvatieri
  • 5,133
  • 1
  • 29
  • 29
1

Your example won't work, since the Extras are made out of a Key and a Value. You cant put multiple Keys and Values in one Extra

Also, your keys need to be Strings as far as I know (and noticed) but I might be wrong on that one.

Try this:

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

Intent i = new Intent(this, CoastList.class);
i.putExtra("ID_Extra", ID_EXTRA);
i.putExtra("1", 111);
startActivity(i);
Sander van't Veer
  • 5,930
  • 5
  • 35
  • 50
0

For a single small amount of value putExtra is perfect but when you pass multiple values and need to pass custom ArrayList or List then I think it's a bad idea. Solution is you can use Parcelable. You and also use Singleton or Serializable also.

Parcelable:

Parcelable class which you need to share.

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

Form your activity where you want to send

MyParcelable example = new MyParcelable();
.....
.....
Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));

From another activity where you want to receive

MyParcelable example = Parcels.unwrap(getIntent().getParcelableExtra("example"));
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60