Android:How to send more than 1 variable with Intent?

670 views Asked by At

Am sending a few variables from main.java:

    Bundle bund = new Bundle();
    Intent intent = new Intent(this, newWindow.class);
    String name = editName.getText().toString();
    bund.putString(yourName, name);
    String pass= editPassword.getText().toString();
    bund.putString(yourPass,pass);
    String mail=EditMail.getText().toString();
    bund.putString(yourMail,mail);
    intent.putExtras(bund);
    startActivity(intent);

to newWindow.java:

        Intent intent = getIntent();
        Bundle extras =  intent.getExtras();
           String name = extras.getString(main.yourName);
           String mail = extras.getString(main.yourMail);
           String pass = extras.getString(main.yourPass);
        viewText1 = (TextView) findViewById(R.id.textView2);
        viewText2 = (TextView) findViewById(R.id.textView3);
        viewText3 = (TextView) findViewById(R.id.textView4);
        viewText1.setText(name);
        viewText2.setText(mail);
        viewText3.setText(pass);

The problem is, only last variable is passed and showed in all 3 textview's

2

There are 2 answers

0
Danish Ansari On

Why not avoid the Bundle part ?

Simply in the FirstActivity, try this

Intent intent = new Intent(this, NewWindow.class);
String name = editName.getText().toString();
String pass= editPassword.getText().toString();
String mail=EditMail.getText().toString();
intent.putExtra("name",name);
intent.putExtra("mail",mail);
intent.putExtra("pass",pass);
startActivity(intent);

Then in RecievingActivity do this

Intent extras=getIntent();
String name = extras.getStringExtra("name");
String mail = extras.getStringExtra("mail");
String pass = extras.getStringExtra("pass");
viewText1.setText(name);
viewText2.setText(mail);
viewText3.setText(pass);

Let me know if it helps

3
CommonsWare On

Presumably, you are using the same key in all cases. In other words, your value of yourName is the same as the value of yourPass, which is the same as the value of yourMail. These need to have unique values. Usually, we use static final String keys for Bundle.