Reputation: 507
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
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
Reputation: 86948
Quite simply: keys allows you to pass more than one String (or Integer, Parcalable, etc) and keep them separate.
Upvotes: 1