今天的破事,我们来聊聊在Android中,如果在两个Activity之间传递对象。
首先我们知道利用Intent的putExtra方法放入要传递的数据。看看API上的函数:
Intent | putExtra( String name, double[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, int value)
Add extended data to the intent.
|
Intent | putExtra( String name, CharSequence value)
Add extended data to the intent.
|
Intent | putExtra( String name, char value)
Add extended data to the intent.
|
Intent | putExtra( String name, Bundle value)
Add extended data to the intent.
|
Intent | putExtra( String name, Parcelable[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, Serializable value)
Add extended data to the intent.
|
Intent | putExtra( String name, int[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, float value)
Add extended data to the intent.
|
Intent | putExtra( String name, byte[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, long[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, Parcelable value)
Add extended data to the intent.
|
Intent | putExtra( String name, float[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, long value)
Add extended data to the intent.
|
Intent | putExtra( String name, String[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, boolean value)
Add extended data to the intent.
|
Intent | putExtra( String name, boolean[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, short value)
Add extended data to the intent.
|
Intent | putExtra( String name, double value)
Add extended data to the intent.
|
Intent | putExtra( String name, short[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, String value)
Add extended data to the intent.
|
Intent | putExtra( String name, byte value)
Add extended data to the intent.
|
Intent | putExtra( String name, char[] value)
Add extended data to the intent.
|
Intent | putExtra( String name, CharSequence[] value)
Add extended data to the intent.
|
Intent | putExtras( Intent src)
Copy all extras in 'src' in to this intent.
|
Intent | putExtras( Bundle extras)
Add a set of extended data to the intent.
|
Intent | putIntegerArrayListExtra( String name, ArrayList< Integer> value)
Add extended data to the intent.
|
Intent | putParcelableArrayListExtra( String name, ArrayList<? extends Parcelable> value)
Add extended data to the intent.
|
Intent | putStringArrayListExtra( String name, ArrayList< String> value)
Add extended data to the intent.
|
Intent | putExtras( Bundle extras)
Add a set of extended data to the intent.
|
一是Bundle.putSerializable(Key,Object);
二是采用Bundle.putParcelable(Key, Object);
其中前面一种方法中的Object要实现Serializable接口,后面一种方法中的Object要实现Parcelable接口。
结合我的项目来说下。首先在你跳转前的Activity设置bundle,这里我是想从
LoginActivity跳转到PersonCenterActivity。
Intent intent = new Intent(); intent.setClass(LoginActivity.this, PersonCenterActivity.class); // 新建Bundle对象 Bundle mBundle = new Bundle(); // 放入account对象 mBundle.putSerializable("account", (AccountInfo)msg.obj); intent.putExtras(mBundle); startActivity(intent);
说明,这里的AccountInfo需要实现Serializable接口。
public class AccountInfo implements Serializable { /** * */ private static final long serialVersionUID = 9060527069391618394L; public String userId; public String accountId; public String phone; public String email; public String password; public String nickName; public String birthday; public String userImage; public String enable; public String mobileOS; public String signIn; public String isSignIn; public String isAuto = "false"; public String msg; public String lastLogin; public String getAccountId() { return accountId; } public void setAccountId(String accountId) { this.accountId = accountId; } public String getSignIn() { return signIn; } public void setSignIn(String signIn) { this.signIn = signIn; } public String getIsSignIn() { return isSignIn; } public void setIsSignIn(String isSignIn) { this.isSignIn = isSignIn; } public void setMsg(String msg) { this.msg = msg; } public void setUserId(String userId) { this.userId = userId; } public void setPhone(String phone) { this.phone = phone; } public void setEmail(String email) { this.email = email; } public void setPassword(String password) { this.password = password; } public void setNickName(String nickName) { this.nickName = nickName; } public void setBirthday(String birthday) { this.birthday = birthday; } public void setUserImage(String userImage) { this.userImage = userImage; } public void setEnable(String enable) { this.enable = enable; } public void setMobileOS(String mobileOS) { this.mobileOS = mobileOS; } public void setLastLogin(String lastLogin) { this.lastLogin = lastLogin; } public String getIsAuto() { return isAuto; } public void setIsAuto(String isAuto) { if (StringUtils.isNotNull(isAuto)) { this.isAuto = isAuto; } else { this.isAuto = "false"; } } public String getUserId() { return userId; } public String getPhone() { return phone; } public String getEmail() { return email; } public String getPassword() { return password; } public String getNickName() { return nickName; } public String getBirthday() { return birthday; } public String getUserImage() { return userImage; } public String getEnable() { return enable; } public String getMobileOS() { return mobileOS; } public String getMsg() { return msg; } public String getLastLogin() { return lastLogin; } public void build(LoginInfo loginInfo) { this.msg = loginInfo.msg; this.accountId = loginInfo.accountId; this.phone = loginInfo.phone; this.email = loginInfo.email; this.password = loginInfo.password; this.nickName = loginInfo.nickName; this.birthday = loginInfo.birthday; this.userImage = loginInfo.userImage; this.enable = loginInfo.enable; this.mobileOS = loginInfo.mobileOS; this.lastLogin = loginInfo.lastLogin; this.signIn = loginInfo.signIn; this.isSignIn = loginInfo.isSignIn; } }
Intent mIntent = new Intent(this,xxx.class); Bundle mBundle = new Bundle(); mBundle.putParcelable("key", account); mIntent.putExtras(mBundle); startActivity(mIntent);
最后我们在PersonCenterActivity中onCreate方法中用下面的方法取得account对象
AccountInfo account = (AccountInfo) getIntent().getSerializableExtra("account");
Parcelable 方式和这基本类似,这里不展开讲了。注意要传递的对象需要实现implements Parcelable。
接受对象:
Account account = (Account)getIntent().getParcelableExtra("key");
好了今天这个破事就到这里,其实android开发就这么点破事。关于其他破事,见专栏:
更多Android开发的破事,请看专栏:《Android开发那点破事》