怎样使用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传递对象的更多相关文章

  1. Android 全局获取 Context 与使用 Intent 传递对象

    =====================全局获取 Context======================== Android 开发中很多地方需要用到 Context,比如弹出 Toast.启动活 ...

  2. Android开发——使用intent传递对象

    intent传递对象有两种方法: 方式一:Serializable 方式 方式二:Parcelable方式 在这里不多介绍了,这一篇就是快速上手使用教程,至于详细原理介绍的,请看这一篇http://w ...

  3. 关于intent传递对象后是传递的对象的地址还是对象的拷贝?

    var intent = Intent(activity,SingleColorControlActivity::class.java); var bundle = Bundle()// bundle ...

  4. Intent传递对象——Serializable和Parcelable差别

    前两篇文章讨论了Serializable和Parcelable实现Intent之间传递对象和对象数组的方式.两种方法实现上相似,效果一致,怎么选择用哪种方法实现呢? Intent在不同的组件中传递对象 ...

  5. Intent传递对象的几种方式

    原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...

  6. android#使用Intent传递对象

    参考自<第一行代码>——郭霖 Intent的用法相信你已经比较熟悉了,我们可以借助它来启动活动.发送广播.启动服务等.在进行上述操作的时候,我们还可以在Intent中添加一些附加数据,以达 ...

  7. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  8. Intent传递对象的两种方法

    Android为intent提供了两种传递对象参数类型的方法 分别需要使实体类实现Serializable接口.Parcelable接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...

  9. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

随机推荐

  1. WebBrowser.ExecWB

      CreateTime--2017年9月22日16:30:10 Author:Marydon js操作浏览器的ExecWB命令 一.准备工作: 以ie浏览器为例,需要更改IE安全级别为:" ...

  2. php抽象类和接口的异同【转】

    1. 相同点: (1)      两者都是抽象类,都不能实例化. (2)      interface 实现类及 abstract class 的子类都必须要实现已经声明的抽象方法. 2. 不同点: ...

  3. Asp.Net MVC中递归死循环问题

    在写代码的时候,很欢乐地发现报错了. An unhandled exception of type 'System.StackOverflowException' occurred in mscorl ...

  4. PowerShell---Operators 介绍

    1.Arithmetic operators(算术运算符) 算术运算符包括加.减.乘.除.取模 此外,加法运算符 (+) 和乘法运算符 (*) 还可对字符串.数组和哈希表进行运算.加法运算符将输入连接 ...

  5. Current thread must be set to single thread apartment (STA) mode before OLE,当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”。

    Add the STAThreadAttribute attribute on the Main method. This attribute is required if your program ...

  6. IOS 颜色 16进制 转换

    #define RGB(r,g,b) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]) #define HEXTO ...

  7. 多线程-CAS原理

    背景 在JDK1.5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁,锁机制存在以下问题: (1)在多线程竞争中,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题 ...

  8. vivado编译出错 [Synth 8-729] [Synth 8-787]

    http://www.cnblogs.com/pejoicen   打开vivado工程后,发现右上角如下图所示: 重新编译这两个ip核后,对整个工程synthesis,工程报错 [Synth 8-7 ...

  9. oracle TABLE ACCESS BY INDEX ROWID 你不知道的索引回表-开发系列(三)

    1 引言 近期系统常常提示一个sql查询时间过长的问题,看了一下就是一个每天依照时间戳统计前一天量的sql. 表总的数据量为53483065. 语句例如以下: select count(x.seria ...

  10. Nginx图片的防盗链配置

    [root@web01 www]# cat /app/server/nginx/conf/vhosts/default.conf server { listen default_server; ser ...