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. Sqlite数据库中的事务

    public void testTrasaction() throws Exception{  PersonSQLiteOpenHelper helper = new PersonSQLiteOpen ...

  2. shell变量(字符串)间的连接

    shell变量(字符串)间的连接 对于变量或者字符串的连接,shell提供了相当简单的做法,比string的连接还要直接. 直接放到一起或用双引号即可. 例如$a, $b,有 c=$a$b c=$a& ...

  3. 《PHP对象、模式与实践》之高级特性

    高级特性包括:1.静态方法和属性(通过类而不是对象来访问数据和功能)2.抽象类和接口(设计,实现分离)3.错误处理(异常)4.Final类和方法(限制继承)5.拦截器(自动委托)6.析构方法(对象销毁 ...

  4. Java中的阻塞和非阻塞IO包各自的优劣思考(经典)

    Java中的阻塞和非阻塞IO包各自的优劣思考 NIO 设计背后的基石:反应器模式,用于事件多路分离和分派的体系结构模式. 反应器(Reactor):用于事件多路分离和分派的体系结构模式 通常的,对一个 ...

  5. mybatis foreach标签的解释 与常用之处

    情景:查询数据库中文章的相关文章   文章为一个表 字段tags为相关文章字符串中间用','逗号进行啦分割 查询完一个文章后可以把tags字段构造为一个List<String> 然后利用这 ...

  6. Rest之路 - 搭建开发环境

    准备Jersey框架和类库 从官网 (https://jersey.java.net/download.html) 下载最新的zip文件,解压后如下图: lib: 包含Jersey的所有类库. ext ...

  7. Nginx反向代理图片总结

    配置需求: 内网192.168.80.205的机器上部署了一个Web项目,下文称web,   url为http://192.168.80.205:8082.   并且使用nginx访问图片,url格式 ...

  8. 主流ETL工具

    主流ETL产品: Ascential公司的Datastage(Datastage在2005年被IBM收购).Informatica公司的Powercenter. NCR Teradata公司的ETL ...

  9. Py修行路 python基础 (十八) 反射 内置attr 包装

    一.isinstance 和 issubclass1.isinstance(obj,cls)检查是否obj是否是类 cls 的对象.2.issubclass(sub, super)检查sub类是否是 ...

  10. 点击jQuery Mobile的按钮改变颜色

    jquery-mobile-移动 我有这样的代码来改变点击一个按钮的颜色: $('.fav').live('click', function(e) { $(this).buttonMarkup({ t ...