Android中activity之间传值是通过Intent来传递数据的。
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。比如,有一个Activity希望打开网页浏览器查看某一网页的内容,那么这个Activity只需要发出WEB_SEARCH_ACTION给Android,Android就会根据Intent的请求内容,查询各组件注册时声明的IntentFilter,找到网页浏览器的Activity来浏览网页。
传递数据的方式:
1.使用Intent转跳
Intent intent = new Intent(activity,目标Activity.class);
2.用Intent传递数据,例如:
intent.putExtra("name","张三");
注意, 这个name表示取值的key,后台是value
3.启动Activity startActivity(intent);
4.在目标Activity中获取这个值
Intent intent = getIntent();
String name = intent.getStringExtra("name");