Min
Min

Reputation: 129

Sending multiple variable values to another activity

So this is the code that I use to transfer the value of a string variable to another activity.

            Intent requestLink = new Intent(Search.this, Results.class);
            requestLink.putExtra("Link", sendLink);
            startActivity(requestLink);

But what if I wish to transfer more than one variable.

            Intent requestLink = new Intent(Search.this, Results.class);
            requestLink.putExtra("Link", sendLink);
            startActivity(requestLink);


            Intent userSearch = new Intent(Search.this, Results.class);
            userSearch.putExtra("Search", addressInput);
            startActivity(userSearch);

Using the code twice will like the above will only just start two separate activities. So, how can I transfer the values simultaneously?

I'm still pretty new to Android development and also OOP.

Upvotes: 4

Views: 16211

Answers (5)

ramin.nvbn
ramin.nvbn

Reputation: 21

Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link1", sendLink1);
requestLink.putExtra("Link2", sendLink2);
startActivity(requestLink);

//Second Activity 

Bundle bundle=getIntent().getExtras();
String Link1 =bundle.getString("Link1");
String Link2 =bundle.getString("Link2");

bundle.get... has many overloads like getInt,... depending on the need.

Upvotes: 2

emraan tamboli
emraan tamboli

Reputation: 31

btnlogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getBaseContext(),db.class);
             String username=uname.getText().toString();
             String upaswrd=pass.getText().toString();
            // Bundle bundle=new Bundle();

          intent.putExtra(name,username);
          intent.putExtra(paswrd, upaswrd);

          startActivity(intent);
        }
    });


/** Db.class */

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.db);
    Intent intent=getIntent();
    String uname=intent.getStringExtra(Gmail.name);
    String upass=intent.getStringExtra(Gmail.paswrd);
    username=(TextView)findViewById(R.id.u);
    username.setText(uname);
    pass=(TextView)findViewById(R.id.p);
    pass.setText(upass);

}

Upvotes: 3

Kirill Rakhman
Kirill Rakhman

Reputation: 43791

Just put both Strings in the same intent.

Intent intent = new Intent(Search.this, Results.class);
intent.putExtra("Link", sendLink);
intent.putExtra("Search", addressInput);

startActivity(intent);

Upvotes: 2

sachy
sachy

Reputation: 789

You can use Bundle for sending data between your Activities. e.g

Intent requestLink = new Intent(Search.this, Results.class);

Bundle bun = new Bundle();
bun.putString("Link",sendLink);
bun.putString("Search", addressInput);

requestLink.putExtras(bun);
startActivity(requestLink);

Check Bundle api documentation here

Upvotes: 2

Philip Sheard
Philip Sheard

Reputation: 5815

You can add more than call putExtra more than once for the same intent:

    Intent requestLink = new Intent(Search.this, Results.class);
    requestLink.putExtra("Link", sendLink);
    requestLink .putExtra("Search", addressInput);
    startActivity(requestLink);

Upvotes: 3

Related Questions