Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。

Intent中传递这2种对象的方法:

Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象

Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象

以下以最常用的Serializable方式为例 :

假设由登录界面(Login)跳转到主界面(MainActivity)传递的对象为登录的用户信息 User类

首先创建一个序列化类:User

import java.io.Serializable;

public class User implements Serializable {
  private int ID;
private String UserName;
private String PWD;
public final void setID(int value)
{
ID = value;
}
public final int getID()
{
return ID;
}
public final void setUserName(String value)
{
UserName = value;
}
public final String getUserName()
{
return UserName;
}
public final void setPWD(String value)
{
PWD = value;
} public final String getPWD()
{
return PWD;
} }

登录窗体登录后传递内容

Intent intent = new Intent();
intent.setClass(Login.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user", user);
intent.putExtras(bundle);
this.startActivity(intent);

接收端

Intent intent = this.getIntent();
user=(User)intent.getSerializableExtra("user");

以上就可以实现对象的传递。

补充:

如果传递的是List<Object>,可以把list强转成Serializable类型,而且object类型也必须实现了Serializable接口

 Intent.putExtras(key, (Serializable)list)  

接收

(List<YourObject>)getIntent().getSerializable(key)

android通过 Intent 传递类对象的更多相关文章

  1. Android 通过 Intent 传递类对象或list对象

    (转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ...

  2. Android 通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  3. Android中Intent传递类对象的方法一(Serializable)

    Activity之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.short ...

  4. Android 开发笔记——通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  5. Android——使用 Intent传递类

    定义要传递的类事,必须加上 public class Movie implements Serializable { } 传入类: public void onItemClick(AdapterVie ...

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

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

  7. 【转】Android中intent传递对象和Bundle的用法

    原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202 android中的组件间传递的对象一般实现Parcelable接口,当然也可 ...

  8. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  9. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

随机推荐

  1. Linux开机挂载windows共享文件夹

    https://blog.csdn.net/zhaogang1993/article/details/79573271  (可行) 命令: mount -t cifs -o username=&quo ...

  2. kubernet使用笔记

    centos7.3下安装及部署:http://www.linuxidc.com/Linux/2017-02/140723.htm

  3. mysq在命令行模式下执行shell命令

    mysql可以在命令行模式下执行shell命令 mysql> help For information about MySQL products and services, visit: htt ...

  4. spring-CXF-maven

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  5. vue-webpack 引入echarts 注意事项

    0.执行教程 https://www.cnblogs.com/goloving/p/8654176.html1.在index 中创建 div <!DOCTYPE html> <htm ...

  6. String类的intern()方法

    0.引言 什么都先不说,先看下面这个引入的例子: String str1 = new String("SEU")+ new String("Calvin"); ...

  7. ssh反向连接内网主机

    holer听别人说也挺好用不过本人没试过:https://github.com/Wisdom-Projects/holer 利用autossh建立稳定隧道,前提双方互加公钥信任. # yum inst ...

  8. hibernate 1对1的关系

    hibernate 中1对1的关系分为外键关联和主键关联 外键关联: //多方 public class Students {                                     ...

  9. conductor介绍

    https://netflix.github.io/conductor/ https://github.com/Netflix/conductor 编译版: https://jcenter.bintr ...

  10. Fiddler 捕获 nodejs 模拟的http请求

    1.设置Fiddler Tools->Options-> Connections Allow remote computers to connect: 2.nodejs 请求有多种 2.1 ...