开发过程中页面和页面之间传递数据示最长遇到的。下面我们来研究下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类:
Parcelable序列化Parcelable序列化需要注意的是,需要重载describeContents()、writeToParcel(Parcel dest, int flags)、Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>()这三个方法。
Parcelable数据传递代码:
Parcelable接收数据代码: