首页 > 移动平台 > 详细

Android学习之Activity跳转与传值

时间:2014-05-13 07:23:29      阅读:437      评论:0      收藏:0      [点我收藏+]

Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据。

 


一、Activity跳转

方法一
Intent intent = new Intent(A.this, B.class); 
startActivity(intent)

 

方法二
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);

实现从A跳转到B(A、B均继承自Activity)

 

 

二、传递数据

Activity A 传递数据

方法一
Intent intent = new Intent();
intent.setClass(A.this, B.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 22);

startActivity(intent);

 

方法二
Intent intent = new Intent(A.this, B.class); 
Bundle bundle = new Bundle();
bundle.putString("name", "xy");
bundle.putInt("age", 22);

intent.putExtras(bundle);
startActivity(intent);

 


Activity B 接收数据


// 获取参数1
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 22); // 缺省值为22

// 获取参数2
Bundle bundle = intent.getExtras();
String name2 = bundle.getString("name");
int age2 = bundle.getInt("age", 22);

两种获取参数方式均可,并不是和传参1,2方法一一对应

Android学习之Activity跳转与传值,布布扣,bubuko.com

Android学习之Activity跳转与传值

原文:http://blog.csdn.net/gisshixisheng/article/details/25597551

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!