Android学习之Activity跳转与传值

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方法一一对应

转载于:https://www.cnblogs.com/lzugis/p/6539905.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值