Android——通过Intent,Bundle在activity间进行数据传递

在安卓activity的数据传递中,Intent和bundle都是用的极多的传递参数的工具。本文就讲介绍Intent与Bundle并简述如何进行参数传递。

1.Intent是什么

Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。

2.Bundle是什么

Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。

我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口。

3.传递数据

传递基本类型的数据主要有2种方式

1、使用intent的putExtra方法

第一个Activity

    Intent intent = new Intent();
    intent.putExtra("value1",value);
    intent.setClass(this,TargetActivity.class);
    startActivity(intent);

其中value变量可以是基本类型数据,甚至是实例对象,当然首先前提要实现Serializable或者Parcelable。

PS:如果传递的对象包含有内部类,也要一并序列化,否则数据是传不过去的。

第二个Activity

     Intent intent = getIntent();
     String value = intent.getStringExtra("value1");

只需要调用响应类型的getXXXXExtra方法就可以了

2、使用Bundle传递

第一个Activity

                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("avatar", user.getAvatar());
                    bundle.putString("username",user.getUsername());
                    bundle.putString("name",user.getName());
                    bundle.putString("gender",user.getGender());
                    bundle.putString("email",user.getEmail());
                    bundle.putString("password",et_pass.getText().toString());
                    intent.putExtras(bundle);
                    intent.setClass(this, TargetActivity.class);
                    startActivity(intent);

这里的bundle直接调用响应类型的put方法了。同样的,取出数据时也要进行相似的操作。

第二个Activity

        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();

        username.setText((String) bundle.get("username"));
        name.setText((String)bundle.get("name"));
        gender.setText((String)bundle.get("gender"));
        email.setText((String)bundle.get("email"));
        userName=(String) bundle.get("username");
        password =(String)bundle.get("password");

这里我偷了个懒直接取出来设置到文本框啦QwQ

3、关于这两种方法的区别

其实。。。。。实际上是一样的。让我们看下Intent的方法

public Intent putExtra(String name, boolean value) {
    if (mExtras == null) { 
        mExtras = new Bundle(); 
    } 
    mExtras.putBoolean(name, value); 
    return this; 
} 

讲道理,其实就是封装了一下吧。。。。。。

而使用Bundle传值的话最后调用的方法:Intent.putExtras(Bundle extras):

public Intent putExtras(Bundle extras) {
    if (mExtras == null) { 
        mExtras = new Bundle(); 
    } 
    mExtras.putAll(extras); 
    return this; 
} 

。。。。。。。。好吧,随意吧。这就是为什么第一个方法里的代码我没有用实际项目里的代码的原因=。=

转载于:https://my.oschina.net/u/3508669/blog/1031513

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值