怎样使用Intent传递对象
怎样使用Intent传递对象
我们能够使用Intent来启动Activity。开启服务Service,发送广播Broadcast,然后使用Intent传递主要的数据类型,如:布尔值,整型,字符串等
Intent intent = new Intent(this, SecondActivyt.class);
intent.putExtra("isBoy", true);
intent.putExtra("age", 24);
intent.putExtra("name", "jane");
假设我们想用Intent传递对象,那么这个传递的对象是可序列化的。Serializable 是序列化的意思,表示将一个对象转换成可存储或可传输的状态。序列化后的对象能够在网络上进行传输,也能够存储到本地。至于序列化的方法也非常easy,仅仅须要让一个类去实现 Serializable 这个接口就能够了。
public class Person implements Serializable {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Person() {
super();
}
public Person(String name, String password) {
super();
this.name = name;
this.password = password;
}
}
这里由于Person 实现了Serialable接口。所以全部的Person对象都是可序列化的。这时我们就能够使用Intent来传递Person对象了。
Person person = new Person("wk","123");
Intent intent = new Intent(this, SecondActivyt.class);
intent.putExtra("person_data", person);
startActivity(intent);
在SecondActivity中,我们就能够获取Person对象了
Person person = (Person) getIntent().getSerializableExtra("person_data");
getSerializableExtra()方法来获取通过參数传递过来的序列化对象,这样我们就获取到了person对象,实现了使用intent传递对象的功能。
怎样使用Intent传递对象的更多相关文章
- Android 全局获取 Context 与使用 Intent 传递对象
=====================全局获取 Context======================== Android 开发中很多地方需要用到 Context,比如弹出 Toast.启动活 ...
- Android开发——使用intent传递对象
intent传递对象有两种方法: 方式一:Serializable 方式 方式二:Parcelable方式 在这里不多介绍了,这一篇就是快速上手使用教程,至于详细原理介绍的,请看这一篇http://w ...
- 关于intent传递对象后是传递的对象的地址还是对象的拷贝?
var intent = Intent(activity,SingleColorControlActivity::class.java); var bundle = Bundle()// bundle ...
- Intent传递对象——Serializable和Parcelable差别
前两篇文章讨论了Serializable和Parcelable实现Intent之间传递对象和对象数组的方式.两种方法实现上相似,效果一致,怎么选择用哪种方法实现呢? Intent在不同的组件中传递对象 ...
- Intent传递对象的几种方式
原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...
- android#使用Intent传递对象
参考自<第一行代码>——郭霖 Intent的用法相信你已经比较熟悉了,我们可以借助它来启动活动.发送广播.启动服务等.在进行上述操作的时候,我们还可以在Intent中添加一些附加数据,以达 ...
- Intent传递对象的两种方法(Serializable,Parcelable) (转)
今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...
- Intent传递对象的两种方法
Android为intent提供了两种传递对象参数类型的方法 分别需要使实体类实现Serializable接口.Parcelable接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
随机推荐
- mysql中select distinct的使用方法
在使用mysql时,有时须要查询出某个字段不反复的记录,尽管mysql提供有distinct这个keyword来过滤掉多余的反复记录仅仅保留一条,但往往仅仅用它来返回不反复记录的条数,而不是用它来返回 ...
- 关于org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor的队列
今天查看源码发现spring的线程池是支持队列的: 并且队列支持的上限相当大: 当线程池的达到最大线程时,默认会把任务放在队列(内存)中,所以我们可以放心用这个东西来写日志了
- CXF调用方式——使用wsdl2java(Windwos下)
1.自动生成客户端代码: 先把CXF下到本地,本例中我下的是apache-cxf-3.1.2,然后在命令行里到相应路径执行命令: D:\soft\DevelopSoft\apache-cxf-\bin ...
- Sphinx-实战
配置完成后, 有了数据源.索引文件存储位置等, 便可以使用 indexer 工具创建索引, 收集要被检索的数据 -c 指定配置文件 默认使用 etc/sphinx.conf --all 对所有索引重新 ...
- C# 中字符串string和字节数组byte[]的转换
string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string: stri ...
- How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载
How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse 原文:http ...
- sql between写法关于查时间区间是否重叠
--判断是否重叠 SELECT * FROM TABLEName WHERE starttime BETWEEN '2008-10-01' AND '2008-10-07' OR endtime BE ...
- 基于numpy的随机数构造
class numpy.random.RandomState(seed=None) RandomState 是一个基于Mersenne Twister算法的伪随机数生成类 RandomState 包含 ...
- ITDB系统搭建及实时备份
ITDB系统搭建及实时备份 ITDB简介 ITDB一款来自希腊的开源IT资产管理系统,它是基于Web的IT资产信息管理系统.对于那些IT设备较多而又缺少管理IT资产信息工具的公司,ITDB是一个不错的 ...
- C1编译器的实现
总览 词法.语法分析 分析方案 词法 语法 符号表 类型系统 AST 语义检查 EIR代码生成器 MIPS代码生成器 寄存器分配 体系结构相关特性优化 使用说明 编译 运行 总览 C1语言编译器及流程 ...