< meta http-equiv="description" content="以前,没怎么认真思考过android的组件间的传值,都是秉承着,设置用shared来存,缓存用文件来存,复杂数据用sqlite,简单的数据之间设置intent,最近,做的一个项目让我认真的思考了一下,我们该如何更方便的处理我们的数据?"/>

浅谈android组件间的值传递

[来源] 达内    [编辑] 达内   [时间]2012-09-18

以前,没怎么认真思考过android的组件间的传值,都是秉承着,设置用shared来存,缓存用文件来存,复杂数据用sqlite,简单的数据之间设置intent,最近,做的一个项目让我认真的思考了一下,我们该如何更方便的处理我们的数据?

浅谈android组件间的值传递

< ul style="margin: 0px 0px 0px 45px; padding: 0px; word-break: break-all; list-style-type: disc; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
  • 导言
  • 5种类型的的简要示例
  • 讨论
  • < h2 style="margin: 15px auto 2px; padding: 0px; font-size: 18px; font-weight: bold; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">导言 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 以前,没怎么认真思考过android的组件间的传值,都是秉承着,设置用shared来存,缓存用文件来存,复杂数据用sqlite,简单的数据之间设置intent,最近,做的一个项目让我认真的思考了一下,我们该如何更方便的处理我们的数据?

    < h2 style="margin: 15px auto 2px; padding: 0px; font-size: 18px; font-weight: bold; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">5种类型的的简要示例 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">假设有这么一个流程

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">A组件 把值传递给 B组件

    < h4 style="margin: 15px auto 2px; padding: 0px; font-size: 13px; font-weight: bold; color: rgb(51, 51, 51); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">内存 < ol style="margin: 0px; padding: 0px 0px 0px 50px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
  • A -> parcel/serializable <- B

    A 利用android的序列化机制,传递值的状态,使B能够通过KEY 获取到A 改变的值.

  • A -> Application <- B

    A 利用Application定义的全局变量,进行值的修改,B通过变量访问Application的值.

  • < h4 style="margin: 15px auto 2px; padding: 0px; font-size: 13px; font-weight: bold; color: rgb(51, 51, 51); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">文件 < ol style="margin: 0px; padding: 0px 0px 0px 50px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
  • A -> sharedpreference <- B

    A 在sharedprefernce 里面更改值,B通过KEY值获取A改变的值

  • A -> File <- B

    A 把值写进文件里面,B通过文件名获取A的值

  • A -> SQlite <- B

    A 通过数据库的操作把值存进去,B同样通过数据库操作获得

  • < h2 style="margin: 15px auto 2px; padding: 0px; font-size: 18px; font-weight: bold; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">讨论 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 对于这5种类型,我以存储的介质,简单做了一个分类,那我们应该怎么对组件间的传值进行一个最优选择呢?

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">还记得,我们最开始学习android的时候,老师都是这么教的

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">`Intent intent = new Intent().setClass(xxxx)

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">context.startxxxx `

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">之后我们的目标类就可以通过获得intent这个包,接着用相应的key获取到相应的值.

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 现在,我们来思考一下这种传值模式.这个模型是一个传递模型,即是一个一对一的传递,如果,我们的关系只有A 组件 到 B 组件,然后B组件到C 组件的话,值的的存在状态只有A 到B,然后B 到 C 传的值是一个全新的状态的话,这种传值模式是非常的简便而且高效.

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 但是,这种模式不利于处理值的状态传递.如果,有这么一个需要,C要读取A发出的值,A的值需要通过B进行处理发给C

    < blockquote style="margin: 10px 0px 10px 25px; padding: 10px 60px 5px; background-image: url(http://common.cnblogs.com/Skins/elf/images/comment.gif); background-color: rgb(255, 255, 255); border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); min-height: 35px; line-height: 1.6em; font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 25px 0px; background-repeat: no-repeat no-repeat; ">

    A -> B -> C

    < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 看上去一点问题都没,但是,通过B 处理的值,C只能接受一次,因为,B传递给C值是一个全新的值,并不是真正的有A那里获取,所以下次要从A获取同样的值,就又要处理一遍,我们可以想象,能够组件关系多了的话这个处理就有多复杂了.这里,我们就会思考,找个地方把值存起来吧.

    < h4 style="margin: 15px auto 2px; padding: 0px; font-size: 13px; font-weight: bold; color: rgb(51, 51, 51); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">值的存储形式 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">对于android的值的保存,我们可以有如下选择

    < ul style="margin: 0px 0px 0px 45px; padding: 0px; word-break: break-all; list-style-type: disc; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
  • Application
  • Sharedpreference
  • File
  • SQlite
  • < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">那么,接下来,我们思考的就是应该以文件的方式存,还是直接把值存到应用的整个周期里面.

    < h4 style="margin: 15px auto 2px; padding: 0px; font-size: 13px; font-weight: bold; color: rgb(51, 51, 51); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">值的生命周期 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">然后,我们需要思考的是我们的值需要存储多久?

    < ul style="margin: 0px 0px 0px 45px; padding: 0px; word-break: break-all; list-style-type: disc; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
  • Application 整个应用程序的存在
  • Sharedpreference 一直存在,直到清除
  • File 一直存在,直到清除
  • SQlite 一直存在,直到清除
  • < h4 style="margin: 15px auto 2px; padding: 0px; font-size: 13px; font-weight: bold; color: rgb(51, 51, 51); font-family: tahoma, arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">值的操作 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">确定了值的存储的周期以后,我们接下来要考虑的就是对于操作

    < ul style="margin: 0px 0px 0px 45px; padding: 0px; word-break: break-all; list-style-type: disc; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
  • Application 通过静态变量
  • Sharedpreference 通过系统的shared框架,用key-value进行值的CURD
  • File 通过文件名
  • SQlite 通过数据库操作
  • 资源下载