简体   繁体   中英

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?

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

非常简单:键允许您传递多个String(或Integer,Parcalable等)并将它们分开。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM