原文链接,非常感谢yayun0516的文章
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。
要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。
Intent中传递这2种对象的方法:
-
Bundle.putSerializable(Key,Object);
//实现Serializable接口的对象
-
-
Bundle.putParcelable(Key, Object);
//实现Parcelable接口的对象
假设由登录界面(Login)跳转到主界面(MainActivity)传递的对象为登录的用户信息 User类
1.首先创建一个序列化类:User
-
package org.yayun.demo;
-
-
import java.io.Serializable;
-
-
import android.R.integer;
-
-
public
class User implements Serializable {
-
private String name;
-
private
int level;
-
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public int getLevel() {
-
return level;
-
}
-
public void setLevel(int level) {
-
this.level = level;
-
}
-
public User(String name,int level) {
-
this.name=name;
-
this.level=level;
-
}
-
-
}
注意点,类实现Serializable接口
2.布局文件很简单,main.xml中一个Button,login.xml中一个TextView:
-
xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width=
"fill_parent"
-
android:layout_height=
"fill_parent"
-
android:orientation=
"vertical" >
-
-
<Button
-
android:id=
"@+id/btn"
-
android:layout_width=
"fill_parent"
-
android:layout_height=
"wrap_content"
-
android:text=
"登录" />
-
-
LinearLayout>
-
xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width=
"fill_parent"
-
android:layout_height=
"fill_parent"
-
android:orientation=
"vertical" >
-
-
<TextView
-
android:id=
"@+id/text"
-
android:layout_width=
"fill_parent"
-
android:layout_height=
"wrap_content"
-
android:text=
"" />
-
-
LinearLayout>
3.MainActivity.java
-
package org.yayun.demo;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public
class MainActivity extends Activity {
-
private Button button;
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.main);
// 设置要使用的布局管理器
-
button=(Button)findViewById(R.id.btn);
-
button.setOnClickListener(
new OnClickListener() {
-
-
public void onClick(View v) {
-
User user=
new User(
"yayun",
7);
//实例化对象
-
Intent intent=
new Intent();
-
intent.setClass(MainActivity.
this, LoginActivity.class);
-
Bundle bundle=
new Bundle();
-
bundle.putSerializable(
"user", user);
//序列化
-
intent.putExtras(bundle);
//发送数据
-
startActivity(intent);
//启动intent
-
-
}
-
});
-
-
}
-
}
4.接收Activity–LoginActivity.java:
-
package org.yayun.demo;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.widget.TextView;
-
-
public
class LoginActivity extends Activity {
-
private TextView textView;
-
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.login);
// 设置要使用的布局管理器
-
textView = (TextView) findViewById(R.id.text);
-
Intent intent =
this.getIntent();
-
User user = (User) intent.getSerializableExtra(
"user");
-
textView.setText(
"用户名:" + user.getName() +
"用户等级"
-
+ String.valueOf(user.getLevel()));
-
-
}
-
}
1.布局文件没有改变,我们看一下MainActivity.java:
-
package org.yayun.demo;
-
-
import java.io.Serializable;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public
class MainActivity extends Activity {
-
private Button button;
-
List
list=
new ArrayList();
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.main);
// 设置要使用的布局管理器
-
button=(Button)findViewById(R.id.btn);
-
button.setOnClickListener(
new OnClickListener() {
-
-
public void onClick(View v) {
-
User user1=
new User(
"yayun",
7);
//实例化对象
-
User user2=
new User(
"feifei",
9);
-
list.add(user1);
-
list.add(user2);
-
Intent intent=
new Intent();
-
intent.setClass(MainActivity.
this, LoginActivity.class);
-
Bundle bundle=
new Bundle();
-
bundle.putSerializable(
"list",(Serializable)list);
//序列化,要注意转化(Serializable)
-
intent.putExtras(bundle);
//发送数据
-
startActivity(intent);
//启动intent
-
-
}
-
});
-
-
}
-
}
2.看一下接收Activity–LoginActivity.java
-
package org.yayun.demo;
-
-
import java.util.List;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.widget.TextView;
-
-
public
class LoginActivity extends Activity {
-
private TextView textView;
-
List
list;
-
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.login);
// 设置要使用的布局管理器
-
textView = (TextView) findViewById(R.id.text);
-
Intent intent =
this.getIntent();
-
list = (List
) intent.getSerializableExtra(
"list");
//获取list方式
-
User user1=list.get(
0);
-
User user2=list.get(
1);
-
textView.setText(
"用户名:" + user2.getName() +
"用户等级"
-
+ String.valueOf(user2.getLevel()));
-
-
}
-
}
1.Bundle.putSerializable(Key,Object); //实现Serializable接口的对象;
2.获取对象User user = (User) intent.getSerializableExtra(“user”);
3.bundle.putSerializable(“list”,(Serializable)list);//对象集合存入方式;
4.list = (List
使用Parcelable
首先创建User.java实现Parcelable接口:
-
package org.yayun.demo;
-
-
import java.io.Serializable;
-
-
import android.R.integer;
-
import android.os.Parcel;
-
import android.os.Parcelable;
-
-
public
class User implements Parcelable {
-
private String name;
-
private
int level;
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getLevel() {
-
return level;
-
}
-
-
public void setLevel(int level) {
-
this.level = level;
-
}
-
-
public User(String name, int level) {
-
this.name = name;
-
this.level = level;
-
}
-
-
public User() {
// 构造方法
-
-
}
-
-
public int describeContents() {
// 覆写的方法
-
return
0;
-
}
-
-
public void writeToParcel(Parcel parcel, int flags) {
// 覆写的方法
-
parcel.writeString(name);
-
parcel.writeInt(level);
-
-
}
-
-
public
static
final Parcelable.Creator
CREATOR =
new Creator() {
//必须实现的方法
-
public User createFromParcel(Parcel source) {
-
User user =
new User();
-
user.name = source.readString();
-
user.level = source.readInt();
-
return user;
-
}
-
-
public User[] newArray(
int size) {
-
return
new User[size];
-
}
-
};
-
-
}
Parcelable.Creator是必须要实现的,否则会报错:
下面看两个Activity
1.MainActivity.java
-
package org.yayun.demo;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public
class MainActivity extends Activity {
-
private Button button;
-
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.main);
// 设置要使用的布局管理器
-
button = (Button) findViewById(R.id.btn);
-
button.setOnClickListener(
new OnClickListener() {
-
-
public void onClick(View v) {
-
User user1 =
new User(
"yayun",
7);
// 实例化对象
-
Intent intent =
new Intent();
-
intent.setClass(MainActivity.
this, LoginActivity.class);
-
Bundle bundle =
new Bundle();
-
bundle.putParcelable(
"user", user1);
// 序列化
-
intent.putExtras(bundle);
// 发送数据
-
startActivity(intent);
// 启动intent
-
-
}
-
});
-
-
}
-
}
2.LoginActivity.java:
-
package org.yayun.demo;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.widget.TextView;
-
-
public
class LoginActivity extends Activity {
-
private TextView textView;
-
User user;
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.login);
// 设置要使用的布局管理器
-
textView = (TextView) findViewById(R.id.text);
-
Intent intent =
this.getIntent();
-
user = (User) intent.getParcelableExtra(
"user");
-
-
textView.setText(
"用户名:" + user.getName() +
"用户等级"
-
+ String.valueOf(user.getLevel()));
-
-
}
-
}
这里我们只需要改动两个Activity
1.MainActivity.java:
-
package org.yayun.demo;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public
class MainActivity extends Activity {
-
private Button button;
-
List
list=
new ArrayList();
-
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.main);
// 设置要使用的布局管理器
-
button = (Button) findViewById(R.id.btn);
-
button.setOnClickListener(
new OnClickListener() {
-
-
public void onClick(View v) {
-
User user1 =
new User(
"yayun",
7);
// 实例化对象
-
User user2=
new User(
"feifei",
9);
-
list.add(user1);
-
list.add(user2);
-
Intent intent =
new Intent();
-
intent.setClass(MainActivity.
this, LoginActivity.class);
-
Bundle bundle =
new Bundle();
-
bundle.putParcelableArrayList(
"list",(ArrayList
)list);
// 序列化,要注意 intent.putExtras(bundle);// 发送数据
-
startActivity(intent);
// 启动intent
-
-
}
-
});
-
-
}
-
}
2.LoginActivity.java
-
package org.yayun.demo;
-
-
import java.util.List;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.widget.TextView;
-
-
public
class LoginActivity extends Activity {
-
private TextView textView;
-
List
list;
-
User user2;
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
// 生命周期方法
-
super.setContentView(R.layout.login);
// 设置要使用的布局管理器
-
textView = (TextView) findViewById(R.id.text);
-
Intent intent =
this.getIntent();
-
list = intent.getParcelableArrayListExtra(
"list");
//getParcelableArrayListExtra方法
-
user2=(User)list.get(
1);
-
textView.setText(
"用户名:" + user2.getName() +
"用户等级"
-
+ String.valueOf(user2.getLevel()));
-
-
}
-
}
最后注意在AndroidManifest.xml文件中配置LoginActivity
1.实现Parcelable接口注意覆写几个方法和实现Parcelable.Creator方法;
2.bundle.putParcelable(“user”, user1);// 序列化
3.user = (User) intent.getParcelableExtra(“user”);//获取对象
4.bundle.putParcelableArrayList(“list”,(ArrayList
5.list = intent.getParcelableArrayListExtra(“list”);//getParcelableArrayListExtra方法获取对象数组
android 中自定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable。 一 序列化原因: 1.永久性保存对象,保存对象的字节序列到本地文件中; 2.通过序列化对象在网络中传递对象; 3.通过序列化在进程间传递对象。 二 至于选取哪种可参考下面的原则: 1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。 2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。 3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。 实现: 1 Serializable 的实现,只需要继承 implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。 2 Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接口。