Activity的传值的几种方式

来源:互联网 发布:php生成8位唯一邀请码 编辑:程序博客网 时间:2024/06/05 07:22

开发过程中页面和页面之间传递数据示最长遇到的。下面我们来研究下activity之间传递数据的几种方式.

1、通过application实现传递数据

2、activity之间的回传值(startActivityForResult())

3、用intent进行传值

一、通过application实现传递数据

BaseApplication中

public class BaseApplication extends Application{
private static final String tag="BaseApplication";
private static BaseApplication mInstance;
public List<String> pathlist = new ArrayList<String>();

@Override
public void onCreate() {
LogUtil.i(tag, "----------onCreate:");
super.onCreate();
mInstance = this;
//initCrashHandle();
}

public static BaseApplication getInstance() {
return mInstance;
}

}

放值进BaseApplication:

        private ArrayList<ImageItem> dataList;

        BaseApplication application = BaseApplication.getInstance();
        application.dataList = dataList;

从BaseApplication中取值:

        private ArrayList<ImageItem> dataList;

        BaseApplication application = BaseApplication.getInstance();
dataList = application.dataList;


二、使用startActivityForResult()进行回传值


跳转的时候用

Intent intentC = new Intent(this, BActivity.class);

/**
 * 跳转目标Activity,并获取返回值
* 第一个参数:意图对象,包含所需要跳转的Activity
* 第二个参数:请求值
 */
startActivityForResult(intentC, REQUEST_CODE_TO_C);

BActivity中适当的时候设置setResult()

Intent resultIntent = new Intent();
resultIntent.putExtra("resutlt", result);

//第一个参数示返回码,第二个参数示intent
setResult(1, resultIntent);

原activity中取数据/做相应的操作

/**
* 在此方法中接受目标Activity返回的值,并进行相应的处理
* 第一个参数:请求码
* 第二个参数:结果码
* 第三个参数:目标Activity返回的Intent对象
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
android.util.Log.e("MainActivity", "onActivityResult");

if (requestCode == REQUEST_CODE_TO_B) {
if (resultCode == 1) {
String content = data.getStringExtra("message");
TextView textView = (TextView) findViewById(R.id.text_Main);
textView.setText("content is : " + content);
} else if(resultCode == -1){
TextView textView = (TextView) findViewById(R.id.text_Main);
textView.setText("content is : " + " 有可能你的请求参数有错误");
}
} else if(requestCode == REQUEST_CODE_TO_C){
if(resultCode == -1){
TextView textView = (TextView) findViewById(R.id.text_Main);
textView.setText("content is : " + "参数有问题");
} else if (resultCode == 1){
String content = data.getStringExtra("msg");
TextView textView = (TextView) findViewById(R.id.text_Main);
textView.setText("content is : " + content);
}

}
}


三、用intent进行传值

intent可以传递5中基本数据类型、Bundle类型、Serialiable对象、Parcrlable对象

intent传递Serialiable对象:

Intent intentD = new Intent(this, DActivity.class);
Bundle bun = new Bundle();
Person p = new Person("jack", 20);
bun.putSerializable(PERSON, p);
intentD.putExtras(bun);
startActivity(intentD);

接收Serialiable对象:

Person p = (Person) getIntent().getExtras().getSerializable(MainActivity.PERSON);
String name = p.name;
int age = p.age;

注:Person类及其中的子类必须实现Serialiable接口,

import java.io.Serializable;


public class Person implements Serializable{

public String name = "";
public int age = 0;

public Person(String name, int age){
this.name = name;
this.age = age;
}


}



Intent传递Parcelable对象

Parcelable序列化了的POJO类:

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.aaron.util;  
  5.   
  6. import android.os.Parcel;  
  7. import android.os.Parcelable;  
  8.   
  9. /** 
  10.  * @author aaron 
  11.  *  
  12.  */  
  13. public class Model implements Parcelable {  
  14.   
  15.     private int month;  
  16.     private float total;  
  17.     private String store;  
  18.   
  19.     /** 
  20.      * @return the month 
  21.      */  
  22.     public int getMonth() {  
  23.         return month;  
  24.     }  
  25.   
  26.     /** 
  27.      * @param month 
  28.      *            the month to set 
  29.      */  
  30.     public void setMonth(int month) {  
  31.         this.month = month;  
  32.     }  
  33.   
  34.     /** 
  35.      * @return the total 
  36.      */  
  37.     public float getTotal() {  
  38.         return total;  
  39.     }  
  40.   
  41.     /** 
  42.      * @param total 
  43.      *            the total to set 
  44.      */  
  45.     public void setTotal(float total) {  
  46.         this.total = total;  
  47.     }  
  48.   
  49.     /** 
  50.      * @return the store 
  51.      */  
  52.     public String getStore() {  
  53.         return store;  
  54.     }  
  55.   
  56.     /** 
  57.      * @param store 
  58.      *            the store to set 
  59.      */  
  60.     public void setStore(String store) {  
  61.         this.store = store;  
  62.     }  
  63.   
  64.     @Override  
  65.     public int describeContents() {  
  66.         // TODO Auto-generated method stub  
  67.         return 0;  
  68.     }  
  69.   
  70.     @Override  
  71.     public void writeToParcel(Parcel dest, int flags) {  
  72.         // TODO Auto-generated method stub  
  73.         dest.writeInt(month);  
  74.         dest.writeString(store);  
  75.         dest.writeFloat(total);  
  76.   
  77.     }  
  78.   
  79.     public static final Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>() {  
  80.   
  81.         @Override  
  82.         public Model createFromParcel(Parcel source) {  
  83.             // TODO Auto-generated method stub  
  84.             Model model = new Model();  
  85.             model.month = source.readInt();  
  86.             model.store = source.readString();  
  87.             model.total = source.readFloat();  
  88.             return model;  
  89.         }  
  90.   
  91.         @Override  
  92.         public Model[] newArray(int size) {  
  93.             // TODO Auto-generated method stub  
  94.             return new Model[size];  
  95.         }  
  96.     };  
  97. }  


Parcelable序列化Parcelable序列化需要注意的是,需要重载describeContents()、writeToParcel(Parcel dest, int flags)、Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>()这三个方法。

 

Parcelable数据传递代码:

[java] view plain copy
  1. //Parcelable实现序列化传送数据  
  2.         Intent intent = new Intent();  
  3.         intent.putParcelableArrayListExtra("Model", (ArrayList<? extends Parcelable>) list);  
  4.           
  5.         intent.setClass(NetClientDemoActivity.this, HelloAchartengineActivity.class);  

 

Parcelable接收数据代码:

[java] view plain copy
  1. //通过Parcelable反序列化获取数据  
  2.         Intent intent = getIntent();  
  3.         List<Model> list = intent.getParcelableArrayListExtra("Model");  
  4.           
  5.         String[] titles = new String[]{list.get(0).getStore()};