標籤:
1. 基礎資料型別 (Elementary Data Type)
Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起點和目標 Bundle bundle = new Bundle(); //建立Bundle對象 bundle.putString("key", "封裝的資料"); //裝入資料 intent.putExtras(bundle); //把Bundle塞入Intent裡面 startActivity(intent);
2. 傳對象的兩種方式 java.io.Serializable和android.os.Parcelable
1. android.os.Parcelable(android推薦使用)
1).被傳遞的類對象需要實現parcelable介面
public class Employee implements Parcelable{ public String id; public String name; public String dept; public String idcard; public String statusInt; public String status; public String mobile; public String sex; public String sexInt; public String address; public String avatar; public String education; public String birthday; public String age; public String dept_name; public String imageUrl; public String manager; public String score; public static final Parcelable.Creator<Employee> CREATOR = new Creator<Employee>() { public Employee createFromParcel(Parcel source) { Employee employee = new Employee(); employee.name = source.readString(); employee.age = source.readString(); employee.dept_name = source.readString(); employee.sex = source.readString(); employee.status = source.readString(); employee.manager = source.readString(); employee.score = source.readString(); employee.imageUrl = source.readString(); return employee; } public Employee[] newArray(int size) { return new Employee[size]; } }; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel parcel, int arg1) { parcel.writeString(name); parcel.writeString(age); parcel.writeString(dept_name); parcel.writeString(sex); parcel.writeString(status); parcel.writeString(manager); parcel.writeString(score); parcel.writeString(imageUrl); } public String getName() { return name; } public void setName(String name) { this.name = name; }}
2)傳遞對象代碼
Intent intent = new Intent(mContext, AnotherActivity.class); Bundle mEmployeeBundle = new Bundle(); mEmployeeBundle.putParcelable(EMPLOYEE_PAR_KEY, mEmployees.get(position)); intent.putExtras(mEmployeeBundle); startActivity(intent);
3) AnotherActivity中取值
mEmployee = (Employee)getIntent().getExtras().getParcelable(HomeFragment.EMPLOYEE_PAR_KEY);Log.d(TAG, "employee name :"+mEmployee.name);
2. java.io.Serializable
1)類對象
public class Ser implements Serializable { /** * serialVersionUID的作用是在修改實體類後,可以正常的序列化和還原序列化,在此不多說,大家可以Google百度下。 */ private static final long serialVersionUID = 123456789090L; private String name; private int age;}
2)傳值及取值
bundle.putSerializable(SER_KEY, new Ser()); intent.putExtras(bundle); startActivity(intent);
pSer = (Ser) getIntent().getSerializableExtra(SER_KEY); //another activity 取值
android intent 傳資料