Intent 可以携带对象来在不同的Activity中传递数据,要传递的数据必须实现三个方法
1. describeContents()返回0就可以 、
2. writeToParcel(Parcel parcel, int flag) 把需要写入的数据写入Parcel中,然后框架调用此方法发送给接收端的Activity
3. public static final Parcelable.Creator<ParcelableObject> CREATOR 外部类反序列化该类时调用此方法
一定要注意:写入parcel和反序列化parcel时顺序一定要相同,不然数据会出错。
?[Copy to clipboard]Download zuiniuwang.java
- /**
- * ParcelableObject.java
- * com.androidtest.parcelable
- *
- * Function: TODO
- *
- * ver date author
- * ──────────────────────────────────
- * 2011-5-11 Leon
- *
- * Copyright (c) 2011, TNT All Rights Reserved.
- */
- package com.androidtest.parcelable;
- import android.os.Parcel;
- import android.os.Parcelable;
- /**
- * ClassName:ParcelableObject 需要实现三个方法,describeContents()返回0就可以 、 writeToParcel(Parcel parcel, int flag)
- * 写入Parcel和public static final Parcelable.Creator
- <parcelableobject><span style="font-size: 14px"> CREATOR 供外部类反序列化该类
- * 一定要注意顺序后面这个两个函数读和写的顺序要相同,不然数据会出错
- * Function: TODO
- * Reason: TODO ADD REASON
- *
- * @author Leon
- * @version
- * @since Ver 1.1
- * @Date 2011-5-11
- */
- public class ParcelableObject implements Parcelable{
- private String name ;
- private String surName ;
- private int age ;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSurName() {
- return surName;
- }
- public void setSurName(String surName) {
- this.surName = surName;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flag) {
- // TODO Auto-generated method stub
- dest.writeInt(age);
- dest.writeString(name);
- dest.writeString(surName);
- }
- public static final Parcelable.Creator CREATOR = new Creator() {
- @Override
- public ParcelableObject createFromParcel(Parcel source) {
- // TODO Auto-generated method stub
- ParcelableObject parcelableObj = new ParcelableObject();
- parcelableObj.age=source.readInt();
- parcelableObj.surName=source.readString();
- parcelableObj.name = source.readString();
- return parcelableObj;
- }
- @Override
- public ParcelableObject[] newArray(int size) {
- // TODO Auto-generated method stub
- return new ParcelableObject[size];
- }
- };
- }
ParcelableActivity1 发送的代码
- package com.androidtest.parcelable;
- 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;
- import android.widget.LinearLayout;
- public class ParcelableActivity1 extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- LinearLayout linearLayout = new LinearLayout(this);
- Button button = new Button(this);
- button.setText("Send Parcelable Object");
- linearLayout.addView(button);
- button.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- ParcelableObject passObject = new ParcelableObject();
- passObject.setName("Leon Zhang");
- passObject.setAge(18);
- passObject.setSurName("xxx");
- Intent intent = new Intent(ParcelableActivity1.this,ParcelableActivity2.class);
- // 使用Bundle发送
- // Bundle bundle = new Bundle();
- // bundle.putParcelable("key", passObject);
- intent.putExtra("key", passObject);
- // intent.putExtras(bundle);
- intent.putExtra("key2", "out of bundle");
- startActivity(intent);
- }
- });
- this.setContentView(linearLayout);
- }
- }
ParcelableActivity2 接收的代码
- package com.androidtest.parcelable;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class ParcelableActivity2 extends Activity {
- private static final String TAG = ParcelableActivity2.class.getSimpleName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- ParcelableObject passObject = this.getIntent()
- .getParcelableExtra("key");
- Log.v(TAG, "Name: " + passObject.getName() + " Surname: "
- + passObject.getSurName() + " Age: "
- + String.valueOf(passObject.getAge())
- + " out of bundle is :"
- + this.getIntent().getStringExtra("key2"));
- Bundle myBundle = this.getIntent().getExtras();
- Log.v(TAG,"XX: "
- + ((ParcelableObject) myBundle.getParcelable("key"))
- .getName()+ " Key2 :"+ myBundle.getString("key2"));
- }
- }
傻蛋经过测试发现,当使用Intent传递数据时,如果你自己创建了一个Bundle,则无论要传的数据是否放入Bundle(如直接intent.putExtra("xx" , "xx")),都会放到放到此Bundle中。如果没有创建此Bundle,则会默认创建一个Bundle来存放数据。当取数据时可以先取Bundle再从Bundle中取数据,或者直接取就好了。
如果要发送对象最简单的方法是: 把要发的数据直接当做对象发送,接收也不用Bundle,因为已经默认创建。
本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/718330,如需转载请自行联系原作者