1.说明
<1>界面数据传递有intent,Intent+bundle两种方式
<2>传递的数据类型又分为基本数据类型,基本数据类型列表,自定义类,自定义类列表四种
<3>序列化有Serializable和Parcelable两种方式。
<4>Serializable序列化只需要将类实现Serializable接口即可;Parcelable序列化类需要实现Parcelable接口,并且重写序列化、非序列化、描述三个方法
<5>Parcelable序列化实例类News在结尾(可参考)
2.1 传递基本类型数据
<1>Intent传递
(1)放:
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
String M=“张三”;
intent.putExtra(“name”,M);
intent.putExtra(“age”,15);
startActivity(intent);
(2)取
Intent intent=getIntent();
Log.d(“177777”,“name=”+intent.getStringExtra(“name”)+“;age=”+intent.getIntExtra(“age”,0));
<2>Intent+Bundle传递
(1)放
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putString(“C”,“C”);
intent.putExtra(“bundle”,bundle);
startActivity(intent);
(2)取
Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“C=”+bundle.getString(“C”));
2.2传递基本数据类型List
<1>Intent传递
(1)放
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
ArrayList M=new ArrayList<>();
M.add(“15”);M.add(“17”);M.add(“25”);
intent.putStringArrayListExtra(“M”,M);
startActivity(intent);
(2)取
Intent intent=getIntent();
Log.d(“177777”,“M=”+intent.getStringArrayListExtra(“M”));
<2>Intent+Bundle传递
(1)放
ArrayList M=new ArrayList<>();
M.add(“15”);M.add(“17”);M.add(“25”);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putStringArrayList(“list”,M);
intent.putExtra(“bundle”,bundle);
startActivity(intent);
(2)取
Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“list=”+bundle.getStringArrayList(“list”));
2.3 传递自定义对象News
<1>Intent传递
[1]News类实现Serializable接口
(1)放
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“news”,new News(“NNNN”));
startActivity(intent);
(2)取
Intent intent=getIntent();
Log.d(“177777”,“News=”+intent.getSerializableExtra(“news”));
[2]News类实现Parcelable接口
(1)放
News news=new News(“BBB”);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“Parcelble”,news);
startActivity(intent);
(2)取
Log.d(“177777”,“news=”+getIntent().getParcelableExtra(“Parcelble”));
<2>Intent+Bundle传递
[1]News类实现Serializable接口
(1)放
News news=new News(“NNNNN”);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putSerializable(“news”,news);
intent.putExtra(“bundle”,bundle);
startActivity(intent);
(2)取
Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“news=”+bundle.getSerializable(“news”));
[2]News类实现Parcelable接口
(1)放
News news=new News(“BBB”);
Bundle bundle =new Bundle();
bundle.putParcelable(“Parcelble”,news);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“bundle”,bundle);
startActivity(intent);
(2)取
Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“news=”+bundle.getParcelable(“Parcelble”));
2.4 传递自定义对象List
<1>Intent传递
[1]News类实现Serializable接口
(1)放
ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“list”,M);
startActivity(intent);
(2)取
Log.d(“177777”,“list=”+getIntent().getSerializableExtra(“list”));
[2]News类实现Parcelable接口
(1)放
ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putParcelableArrayListExtra(“list”,M);
startActivity(intent);
(2)取
Log.d(“177777”,“list=”+getIntent().getParcelableArrayListExtra(“list”));
<2>Intent+Bundle传递
[1]News类实现Serializable接口
(1)放
ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putSerializable(“news”,M);
intent.putExtra(“bundle”,bundle);
startActivity(intent);
(2)取
Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“news=”+bundle.getSerializable(“news”));
[2]News类实现Parcelable接口
(1)放
ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle=new Bundle();
bundle.putParcelableArrayList(“list”,M);
intent.putExtra(“bundle”,bundle);
startActivity(intent);
(2)取
Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“list=”+bundle.getParcelableArrayList(“list”));
2.5 Parcelable序列化示例类News
package com.example.xianchengtest;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
public class News implements Parcelable {
public String content;
public String getContent() {
return content;
}
public News(String content) {
this.content = content;
}
protected News(Parcel in) {
content = in.readString();
}
/*
* 序列化
* */
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(content);
// 序列化自定义类
// parcel.writeParcelable("自定义类");
}
/*
* 反序列化
* */
public static final Creator<News> CREATOR = new Creator<News>() {
@Override
public News createFromParcel(Parcel in) {
return new News(in);
}
@Override
public News[] newArray(int size) {
return new News[size];
}
};
/*
* 描述
* */
@Override
public int describeContents() {
return 0;
}
}