栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android activity之间的跳转

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android activity之间的跳转

1 直接跳转
这里有两个Activity:FirstActivity与SecondActivity,从FirstActivity中跳转到SecondActivity中只需要一个startActivity()即可:

startActivity(new Intent(this,SecondActivity.class));
 

携带数据

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

intent.putExtra("name", "xxx");

......

......

startActivity(intent);

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

Bundle bundle = new Bundle();

bundle.putString("name", "xxx");

......

......

intent.putExtras(bundle);

startActivity(intent);

注意:intent.putExtra(String key, value);这个方法里写入的是键值对,key为String类型,value基本囊括了所有基本类型,可填入泛型数据。而bundle则是填入具体类型的数据。

B Activity中接收数据

Intent intent = this.getIntent();

String name = intent.getStringExtra("name");

int age = intent.getIntExtra("age", 18); // 缺省值为22

Bundle bundle = intent.getExtras();

String name = bundle.getString("name");

int age = bundle.getInt("age", 18);

注意:以上两种获取参数方式均可,跟A Activity传参时所调用的方法(intent.putExtra(Bundle bundle)或intent.putExtra(String key, value))无关

注意:通过调用startActivity(intent)跳转的界面是无法返回数据的,只能由A Activity单向传值到B Activity。

//以后学了再来补充

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/284233.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号