Intent 跳转

Intent跳转方式详解:从基本到进阶
本文深入探讨了Intent在Android应用开发中的跳转方式,从直接界面跳转到参数传递,再到使用Bundle和序列化对象的高级用法。详细解释了每种方法的实现步骤和应用场景。

Intent 的几种跳转方式
第一种 直接界面A跳转到界面B

Intent intent = new Intent(A类名.this, B类名.class);
startActivity(intent);

第二种 界面A跳转到界面B传一个字符串
A界面代码

Intent intent = new Intent(A类名.this, B类名.class);
intent.putExtra("字符串的名字", "字符串的内容");
startActivity(intent);

B界面代码

Intent intent=getIntent();
String str=intent.getStringExtra("A中定义字符串的名字");
A中字符串的内容保存到str

第三种 利用bundle传字符串
Intent intent = new Intent(A类名.this, B类名.class);
Bundle bundle=new Bundle();
bundle.putString(“key”, “value”);
intent.putExtra(“bundle”, bundle);
startActivity(intent);
接收

Intent intent=getIntent();
Bundle bundle=intent.ge tBundleExtra("bundle");
System.out.println(bundle.getString("key"));

第四种 传输对象User 定义的时候最好是实现序列化
A界面代码

Intent intent = new Intent(A类名.this, B类名.class);
Bundle bundle=new Bundle();
bundle.putString("info", "value");
User use=new User("xiaohai",20);
bundle.putSerializable("user", use);
intent.putExtra("bundle", bundle);
startActivity(intent);

B界面代码

Intent intent=getIntent();
Bundle bundle=intent.getBundleExtra("bundle");
User use=(User)bundle.getSerializable("user");
System.out.println(bundle.getString("info")+""+use.toString());

最后记得要在清单文件 AndroidManifest.xml 中添加

<activity     
android:name="com.example.包名.B类"
android:label="@string/app_name" >
</activity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值