gfree
gfree

Reputation: 507

Why does putExtra take a key value pair and not just a value?

I am going through some of the Android development tutorials, and I just came up with this general question:

Why is it that the intent.putExtra method takes a key value pair and not just a value?

If I want to pass a string to the new Intent, why not just pass the String? What benefits does the key have?

Upvotes: 2

Views: 2642

Answers (2)

tbraun89
tbraun89

Reputation: 2234

You can add more than one object to your Intent so you could do this:

intent.putExtra("name", "My Name");
intent.putExtra("age", 30);

If you want to get the some of the data back you need to specify which using the key:

intent.getStringExtra("name"); // returns "My Name"
intent.getIntExtra("age");     // returns 30

Upvotes: 4

Sam
Sam

Reputation: 86948

Quite simply: keys allows you to pass more than one String (or Integer, Parcalable, etc) and keep them separate.

Upvotes: 1

Related Questions