Android 五种数据传递方法汇总 2014-01-24 18:42:13| 分类: 默认分类|举报|字号 订阅 Android 开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递,我把常用的几种 方法都收集到了一起
它们各有利弊,有各自的应用场景
我现在把它们集中到一个例子中展示,在例子中每一个按纽代表了一种实现方法
利用Intent 对象携带简单数据 利用Intent 的Extra 部分来存储我们想要传递的数据,可以传送 int, long, char等一些基础类型,对复杂的对象就无能为力了
1 设置参数 [java] //传递些简单的参数 Intent intentSimple = new Intent(); intentSimple
setClass(MainActivity
this,SimpleActivity
class); Bundle bundleSimple = new Bundle(); bundleSimple
putString("usr", "xcl"); bundleSimple
putString("pwd", "zj"); intentSimple
putExtras(bundleSimple); startActivity(intentSimple); 1
2 接收参数 [java] this
setTitle("简单的参数传递例子"); //接收参数 Bundle bunde = this
getIntent()
getExtras(); String eml = bunde
getString("usr"); String pwd = bunde
getString("pwd"); 2
利用Intent 对象携带如ArrayList 之类复杂些的数据 这种原理是和上面一种是一样的,只是要注