android intent 传数据

1. 基本数据类型

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的作用是在修改实体类后,可以正常的序列化和反序列化,在此不多说,大家可以谷歌百度下。
     */
    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 取值 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。