Intent传递对象(两种序列化方式Serializable/Parcelable)

     



    intent主要用在Activity与Activity之间的数据传递。除了我们平时比较经常用得简单数据传递,还可以将整一个类传递给另外一个Activity,只要这个将这个类序列化即可。这里自己敲了一个demo跟大家分享一下,希望对新手有所帮助。高手可以无视本帖。
    MainActivity 
    1. public class MainActivity extends Activity implements OnClickListener {
    2.         private final int SERIALIZABLE = 1;
    3.         private final int PARCELABLE = 2;
    4.         private Button SerializableBtn;
    5.         private Button ParcelableBtn;
    6.         private Person p;
    7.         private Person2 p2;

    8.         @Override
    9.         protected void onCreate(Bundle savedInstanceState) {
    10.                 super.onCreate(savedInstanceState);
    11.                 setContentView(R.layout.activity_main);
    12.                 initView();
    13.         }

    14.         private void initView() {
    15.                 SerializableBtn = (Button) findViewById(R.id.serializable);
    16.                 ParcelableBtn = (Button) findViewById(R.id.parcelable);
    17.                 SerializableBtn.setOnClickListener(this);
    18.                 ParcelableBtn.setOnClickListener(this);
    19.         }

    20.         private void initSerializableDate() {
    21.                 p = new Person();
    22.                 p.setName("Changel");
    23.                 p.setPassword("123456789");
    24.         }

    25.         private void initParcelablebleDate() {
    26.                 p2 = new Person2();
    27.                 p2.setName("Changel");
    28.                 p2.setPassword("123456789");
    29.         }

    30.         @Override
    31.         public void onClick(View v) {
    32.                 switch (v.getId()) {
    33.                 case R.id.serializable:
    34.                         initSerializableDate();
    35.                         Intent i = new Intent();
    36.                         Bundle bundle = new Bundle();
    37.                         bundle.putSerializable("person", p);
    38.                         i.putExtras(bundle);
    39.                         i.setAction("com.example.objecttrandemo.intent.action.serializable");
    40.                         startActivityForResult(i, SERIALIZABLE);
    41.                         break;
    42.                 case R.id.parcelable:
    43.                         initParcelablebleDate();
    44.                         Intent i2 = new Intent();
    45.                         Bundle bundle2 = new Bundle();
    46.                         bundle2.putParcelable("person", p2);
    47.                         i2.putExtras(bundle2);
    48.                         i2.setAction("com.example.objecttrandemo.intent.action.parcelable");
    49.                         startActivityForResult(i2, PARCELABLE);
    50.                         break;
    51.                 }
    52.         }

    53.         @Override
    54.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    55.                 super.onActivityResult(requestCode, resultCode, data);
    56.                 switch (requestCode) {
    57.                 case SERIALIZABLE:
    58.                         Toast.makeText(MainActivity.this, "实现serializable接口传递成功", Toast.LENGTH_SHORT).show();
    59.                         break;
    60.                 case PARCELABLE:
    61.                         Toast.makeText(MainActivity.this, "实现parcelable接口传递成功", Toast.LENGTH_SHORT).show();
    62.                         break;
    63.                 }
    64.         }

    65.         @Override
    66.         public boolean onCreateOptionsMenu(Menu menu) {
    67.                 // Inflate the menu; this adds items to the action bar if it is present.
    68.                 getMenuInflater().inflate(R.menu.main, menu);
    69.                 return true;
    70.         }
    71. }
    复制代码
    ParcelableActivity 
    1. public class ParcelableActivity extends Activity{
    2.         private TextView tv;
    3.         @Override
    4.         protected void onCreate(Bundle savedInstanceState) {
    5.                 super.onCreate(savedInstanceState);
    6.                 setContentView(R.layout.test_activity);
    7.                 tv = (TextView) findViewById(R.id.textView1);
    8.                 Person2 p2 = (Person2) getIntent().getParcelableExtra("person");
    9.                 tv.setText("姓名:"+p2.getName()+"\n密码:"+p2.getPassword());
    10.                 setResult(Activity.RESULT_OK);
    11.         }
    12. }
    复制代码

    SerializableActivity 
    1. public class SerializableActivity extends Activity {
    2.         private TextView tv;
    3.         @Override
    4.         protected void onCreate(Bundle savedInstanceState) {
    5.                 super.onCreate(savedInstanceState);
    6.                 setContentView(R.layout.test_activity);
    7.                 tv = (TextView) findViewById(R.id.textView1);
    8.                 Person p = (Person) getIntent().getSerializableExtra("person");
    9.                 tv.setText("姓名:"+p.getName()+"\n密码"+p.getPassword());
    10.                 setResult(Activity.RESULT_OK);
    11.         }
    12. <font size="2">}</font>
    复制代码
    Person 

    1. public class Person implements Serializable{
    2.         String name;
    3.         String password;

    4.         public String getName() {
    5.                 return name;
    6.         }

    7.         public String getPassword() {
    8.                 return password;
    9.         }

    10.         public void setName(String name) {
    11.                 this.name = name;
    12.         }

    13.         public void setPassword(String password) {
    14.                 this.password = password;
    15.         }
    16. <font size="5"><font size="2">}</font>
    17. </font>
    复制代码
    Person2 
    1. public class Person2 implements Parcelable {
    2.         String name;
    3.         String password;

    4.         public String getName() {
    5.                 return name;
    6.         }

    7.         public String getPassword() {
    8.                 return password;
    9.         }

    10.         public void setName(String name) {
    11.                 this.name = name;
    12.         }

    13.         public void setPassword(String password) {
    14.                 this.password = password;
    15.         }

    16.         public static final Parcelable.Creator<Person2> CREATOR = new Creator<Person2>() {
    17.                 public Person2 createFromParcel(Parcel source) {
    18.                         Person2 person2 = new Person2();
    19.                         person2.setName(source.readString());
    20.                         person2.setPassword(source.readString());
    21.                         return person2;
    22.                 }

    23.                 @Override
    24.                 public Person2[] newArray(int size) {
    25.                         return new Person2[size];
    26.                 }
    27.         };

    28.         @Override
    29.         public int describeContents() {
    30.                 return 0;
    31.         }

    32.         @Override
    33.         public void writeToParcel(Parcel dest, int flags) {
    34.                 dest.writeString(name);
    35.                 dest.writeString(password);

    36.         }

    37. }
    复制代码
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值