1.传递普通数据

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle=new Bundle();
bundle.putString("name","张三");
bundle.putInt("age",18);
bundle.putString("gender","男");
intent.putExtras(bundle);
startActivity(intent);

获取传递的数据

        Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
String gender=bundle.getString("gender");
int age =bundle.getInt("age");

2.传递Serializable数据

1.创建一个类实现Serializable

2.传递数据

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle=new Bundle();
Person1 p1=new Person1("张三","男",18);
bundle.putSerializable("person",p1);
intent.putExtras(bundle);
startActivity(intent);

3.接受数据

        Bundle bundle=getIntent().getExtras();
Person1 p1= (Person1) bundle.getSerializable("person");
String name=p1.getName();
String gender=p1.getGender();
int age =p1.getAge();

3.传递Parcelable数据

1.创建类实现Parcelabel

public class Person3 implements Parcelable {
private String name;
private String gender;
private int age; public Person3(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
} public String getName() {
return name;
} public String getGender() {
return gender;
} public int getAge() {
return age;
} @Override
public String toString() {
return "Person3{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
} public static final Parcelable.Creator<Person3> CREATOR=new Parcelable.Creator<Person3>(){ /**
* 供外部类反序列话本类数组使用
* @param source
* @return
*/ @Override
public Person3 createFromParcel(Parcel source) {
return new Person3(source);
} /**
* 从Parcel中读取数据
* @param size
* @return
*/
@Override
public Person3[] newArray(int size) {
return new Person3[size];
}
}; /**
* 默认返回0就行
* @return
*/
@Override
public int describeContents() {
return 0;
} /**
* 把值写进Parcel中
* @param dest
* @param flags
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(gender);
dest.writeInt(age);
} /**
* 这里的读取数据必须与writeToParacel(Parcel dest,int flags)一致,否则就会出错
* @param source
*/
public Person3(Parcel source) {
name = source.readString();
gender=source.readString();
age = source.readInt();
}
}

2.传递数据

                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
Bundle bundle=new Bundle();
Person3 p3=new Person3("张三","男",18);
bundle.putParcelable("person",p3);
intent.putExtras(bundle);
startActivity(intent);

3.接受数据

     Bundle bundle=getIntent().getExtras();
Person3 p3= bundle.getParcelable("person");
String name=p3.getName();
String gender=p3.getGender();
int age =p3.getAge();

Android Bundle传递数据的更多相关文章

  1. android bundle存放数据详解

    转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...

  2. Bundle传递数据,Handler更新UI

    Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean.byte.in ...

  3. Android Bundle传递简单数据、对象数据

    Android开发过程中进程遇到组件之间.进程之间等数据的传递,数据传递有非常多种,当中使用Bundle传递非常方便. Bundle能够传递多种数据,是一种类似map的key-value数据结构 简单 ...

  4. Android之间传递数据包

    在Android中 ,我们知道,两个activity之间通讯要用到Intent类,传递简单数据的方式我们也已经知道了.那么,如何在两个activity之间传递数据包呢,这就要用到我们的Bundle类了 ...

  5. 关于Android中传递数据的一些讨论--备用

    在Android中编写过程序的开发人员都知道.在Activity.Service等组件之间传递数据(尤其是复杂类型的数据)很不方便.一般可以使用Intent来传递可序列化或简单类型的数据.看下面的代码 ...

  6. 关于Android中传递数据的一些讨论

    在Android中编写过程序的开发人员都知道.在Activity.Service等组件之间传递数据(尤其是复杂类型的数据)很不方便.一般可以使用Intent来传递可序列化或简单类型的数据.看下面的代码 ...

  7. Android开发—— 传递数据

    一:使用静态变量传递数据 (1)静态变量传递数据,在目标Activity中声明静态变量,然后使用setText()方法将静态变量的值导出即可: (2)静态变量传递数据,在主Activity中对目标Ac ...

  8. Activity通过bundle传递数据

    从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...

  9. Android Intent传递数据

    刚开始看郭大神的<>,实现以下里面的一些例子.Intent传递数据. 我们利用显示的方式进行Intent的启动. 1.启动intent并输入数据. Intent intent=new In ...

随机推荐

  1. Java Integer和String内存存储

    标签: java内存string 2016-01-10 12:51 1545人阅读 评论(2) 收藏 举报  分类: Java(7)  版权声明:本文为博主原创文章,未经博主允许不得转载. 先看代码: ...

  2. jQuery选择器this通过onclick传入方法以及Jquery中的this与$(this)初探,this传处变量等

    起初以为this和$(this)就是一模子刻出来.但是我在阅读时,和coding时发现,总不是一回事. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  3. Netty Client 重连实现

    当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连.Netty Client有两种情况下需要重连: Netty Client启动的时候需要重连 在程序 ...

  4. Java-Runoob:Java 条件语句

    ylbtech-Java-Runoob:Java 条件语句 1.返回顶部 1. Java 条件语句 - if...else 一个 if 语句包含一个布尔表达式和一条或多条语句. 语法 if 语句的语法 ...

  5. PHP中静态变量的使用

    1.定义静态变量 public static $endpoint,$accessKeyId,$accessKeySecret,$bucket; 2.静态变量赋值 protected function ...

  6. matlab神经网络工具箱创建神经网络

    为了看懂师兄的文章中使用的方法,研究了一下神经网络 昨天花了一天的时间查怎么写程序,但是费了半天劲,不能运行,百度知道里倒是有一个,可以运行的,先贴着做标本 % 生成训练样本集 clear all; ...

  7. 软件部需求,内容采集,显示内容图文列表,MongoDB数据导入导出JSON

    全局变量 由于多个html页面,需要引用同一个变量.这个时候,需要定义一个全局变量!如何定义呢? 默认包含了mui的html文件都导入mui.js文件.那么将变量写在mui.js中,就可以实现所有页面 ...

  8. quarz入门案例

    介绍 Quartz框架是一个全功能.开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型的电子商务系统.Quartz可以执行上千上万的任务调度.   核心概念  Quartz ...

  9. Hbase 一次表异常,有一张表 无法count scan 一直显示重连

    z_activeagent z_weekstore z_wstest zz_monthstore row(s) in 0.5240 seconds => ["KYLIN_02YJ3NJ ...

  10. 一个简单的AXIS远程调用Web Service示例

    我们通常都将编写好的Web Service发布在Tomcat或者其他应用服务器上,然后通过浏览器调用该Web Service,返回规范的XML文件.但是如果我们不通过浏览器调用,而是通过客户端程序调用 ...