2011.07.19——— android intent 传递list或者对象

参考:http://blog.csdn.net/Android_Tutor/article/details/5740845
http://blog.csdn.net/onerain88/article/details/6562464
http://www.eoeandroid.com/thread-163-2-1.html


方法一:
如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用

intent.putStringArrayListExtra(name, value)intent.putIntegerArrayListExtra(name, value)



方法二:
如果传递的是List<Object>,可以把list强转成Serializable类型,然后通过
putExtras(key, (Serializable)list)

方法传递过去,接受的时候用
(List<YourObject>) getIntent().getSerializable(key)

就可以接受到List<YourObject>数据了

但是 切记 你的YourObject类必须要实现Serializable接口

方法三:
一种是
Bundle.putSerializable(Key,Object);

另一种是
Bundle.putParcelable(Key, Object);

当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口


方法四:
用intent传来传去 觉得不方便 我们可以写一个在application里面的全局数据

1、创建一个属于你自己的android.app.Application的子类
2、在manifest中申明一下这个类,
3、这时android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。

继承Application
class MyApp extends Application {  private String myState;  public String getState(){    return myState;  }  public void setState(String s){    myState = s;  }}


关于AndroidManifest.xml中的配置,原来直接给application加个name就可以了,如下面所示:
<application android:name=".MyApp"         android:icon="@drawable/icon"android:label="@string/app_name">


使用
class Blah extends Activity {  @Override  public void onCreate(Bundle b){    ...    MyApp appState = ((MyApp)getApplicationContext());    String state = appState.getState();    ...  }}







更多相关文章

  1. 【翻译】(8-补丁1)Android接口定义语言(AIDL)
  2. 【Fragment】 Android(安卓)Fragment生命周期详解(图文)
  3. android唤醒屏幕--保持屏幕唤醒-Reprinted
  4. Qt for Android获取手机序列号
  5. 【Android】Android中的数据传递(2)
  6. android之wifi体系架构源码流程分析
  7. [Android]设置Activity为全屏显示的两种方法
  8. android 开发-Process and Thread
  9. js 与安卓或ios 的交互传参

随机推荐

  1. android 敏捷开发系列(一)——《啥是敏捷开
  2. android 自动化测试工具 Monkey
  3. [基础知识]全面认识AndroidManifest.xml
  4. 关于android中使用new Message的内存泄露
  5. android重要包的描述
  6. android动态创建控件
  7. Android近期任务列表 Recent Applicatoin
  8. Android屏幕适配攻略
  9. Android(安卓)-- Looper.prepare()和Loop
  10. Android项目--团购类客户端