怎样使用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. c++11 学习

    #include <iostream> // std::cout #include <functional> // std::ref #include <thread&g ...

  2. Linux-查看进程的完整路径

    通过ps及top命令查看进程信息时,只能查到相对路径,查不到的进程的详细信息,如绝对路径等.这时,我们需要通过以下的方法来查看进程的详细信息:Linux在启动一个进程时,系统会在/proc下创建一个以 ...

  3. FTP服务搭建与配置

    FTP介绍 大企业用的基本都是自动化发布工具,会用GIT企业发布的版本上传到服务器, 使用vsftpd搭建ftp服务(上) http://blog.csdn.net/qq_26941173/artic ...

  4. HTTP Cache怎样计算Age

    这里的Age指的是响应头Age.以下内容有部分翻译,也有部分自己的理解.欢迎讨论. 我们用now表示当前主机的当前时间,用request_time表示缓存发起请求的时间,用response_time表 ...

  5. sklearn基本回归方法

    https://blog.csdn.net/u010900574/article/details/52666291 博主总结和很好,方法很实用. python一些依赖库: https://www.lf ...

  6. ssh理论知识

    一.spring工作原理: 1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作. 2.DispatcherSer ...

  7. Java并发编程(九):拓展

    java多线程死锁理解 Java多线程并发最佳实践 Spring与线程安全 HashMap与ConcurrentHashMap 关于java集合类HashMap的理解     ,      数据结构之 ...

  8. Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#

    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c# 1. 加密算法的参数::算法/模式/填充 1 2. 标准加密api使用流程1 2.1. Md5——16bi ...

  9. JSP中out.write()和out.print()的区别

    out对象的类型是JspWriter.JspWriter继承了java.io.Writer类. 1)print方法是子类JspWriter,write是Writer类中定义的方法: 2)重载的prin ...

  10. 171. Anagrams【medium】

    Given an array of strings, return all groups of strings that are anagrams. Notice All inputs will be ...