【Android开发那点破事】Android2个Activity之间传递对象
来源:互联网 发布:妹子语音软件 编辑:程序博客网 时间:2024/05/22 05:06
今天的破事,我们来聊聊在Android中,如果在两个Activity之间传递对象。
首先我们知道利用Intent的putExtra方法放入要传递的数据。看看API上的函数:
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Copy all extras in 'src' in to this intent.
Add a set of extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
Add extended data to the intent.
从上面的方法发现,没有直接放入object的函数。但是有一个
Add a set of extended data to the intent.
没错,我们就是用Intent.putExtras(Bundle extras)来传递对象。生成Bundle对象时有两种放入对象的方式一是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开发那点破事》原创链接:http://blog.csdn.net/withiter/article/details/16946659
0 0
- 【Android开发那点破事】Android2个Activity之间传递对象
- 【Android开发那点破事】Android2个Activity之间传递对象
- Android2个Activity之间传递对象
- Android2个Activity之间传递对象
- Android2个Activity之间传递对象
- 【Android开发那点破事】Android中Activity的生命周期
- 【Android开发那点破事】Android调试安装出现2个或2个以上图标
- 【Android开发那点破事】Android调试安装出现2个或2个以上图标
- 【Android开发那点破事】消息推送BroadcastReceiver,点击通知打开两次Activity问题
- 【Android开发那点破事】Web程序员轻松使用PhoneGap开发第一个Andriod程序
- 【Android开发那点破事】防止Android apk被反编译
- 【Android开发那点破事】Android判断SD卡状态
- 【Android开发那点破事】解决android.os.NetworkOnMainThreadException
- [Android开发那点破事]解决android.os.NetworkOnMainThreadException
- [Android开发那点破事]解决android.os.NetworkOnMainThreadException
- [Android开发那点破事]解决android.os.NetworkOnMainThreadException
- [Android开发那点破事]解决android.os.NetworkOnMainThreadException
- Android Activity 之间传递对象
- Mac OS X 10.9 启用 Web 服务器
- 移动 网通 联通 电信
- android adt-bundle环境搭建
- ios 含category的静态库出现selector not recognized错误的解决办法
- c++类的实例化
- 【Android开发那点破事】Android2个Activity之间传递对象
- nodejs开发指南里面dynamicHelpers函数问题解决方法
- openfire外网登录有点慢
- Built-in custom editor styles in Unity3D
- HDU 4521 小明系列问题——小明序列
- 手机连不上无线网
- MonkeyRunner入门
- 怎样解决电信网通南北互通问题?
- ubuntu下的解压缩命令