大家都知道,在Android 的Activity之间跳转是可以传递参数的,可以通过Intent,或者Intent结合Bundle进行传参,如果要传递的参数是一个对象类型的参数,那么该对象必须要实现Serializable接口或者实现Parcelable接口,对于实现Serializable接口,没什么可说的,直接实现该接口就可以了.本文主要介绍如何通过实现Parcelable接口来完成Activity间的对象类型参数的传递.
Demo的结构,如下:
MainActivity通过Intent打开Main2Activity,同时会传递一个Person对象,显示在Main2Activity的界面上.大体步骤如下:
步骤1:创建一个Person对象,即要传递的参数
package bean;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mChenys on 2015/10/31.
*/
public class Person implements Parcelable {
private String name;
private String address;
private int age;
private List<String> hobby;
public Person() {
}
public Person(String name, String address, int age, List<String> hobby) {
this.name = name;
this.address = address;
this.age = age;
this.hobby = hobby;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString("name", getName());
bundle.putString("address", getAddress());
bundle.putInt("age", getAge());
bundle.putStringArrayList("hobby", (ArrayList<String>) getHobby());
dest.writeBundle(bundle);
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
Bundle bundle = source.readBundle();
return new Person(bundle.getString("name"),
bundle.getString("address"),
bundle.getInt("age"),
bundle.getStringArrayList("hobby"));
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
步骤2:创建MainActivity
MainActivity的布局很简单,就一个Button,onClick=goNext
package com.example.mchenys.parcelabledemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import bean.Person;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 通过Button点击打开Main2Activity,传递参数
* @param view
*/
public void goNext(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
List<String> hobby = new ArrayList<>();
hobby.add("game");
hobby.add("swimming");
hobby.add("movies");
intent.putExtra("data", new Person("zhangsan", "gz", 24, hobby));
startActivity(intent);
}
}
步骤3:创建Main2Activity,通过TextView展示接收到Person对象的数据.package com.example.mchenys.parcelabledemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import bean.Person;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
Person p = intent.getParcelableExtra("data");
TextView textView = (TextView) findViewById(R.id.id_tv_info);
textView.setText(String.format("name=%s,address=%s,age=%d,hobby=%s", p.getName(), p.getAddress(), p.getAge(),p.getHobby()));
}
}
效果图: