昨天上课学习Activity之间的数据传输,并学习了对EditText一键删除的实现。用到的方式有
- Intent直接传输
- Bundle打包用intent传输
- a.Serializable+bundle+intent实现的数据传递
b. 用Parcelable+Bundle+intent来实现
听说特别牛逼的是用Parcelable来传输,最牛逼的是听说能跨进程
Android的Parcelable的设计初衷是因为Serializable效率过慢,为了在程序内不同组件间以及不同Android程序间(AIDL)高效的传输数据而设计,这些数据仅在内存中存在,Parcelable是通过Bindle通信的消息的载体。
Parcelable的性能比Serializable好,在内存开销方面较小,所以在内存间数据传输时推荐使用Parcelable,如activity间传输数据,而Serializable可将数据持久化方便保存,所以在需要保存或网络传输数据时选择Serializable,因为android不同版本Parcelable可能不同,所以不推荐使用Parcelable进行数据持久化。
Parcelable的主要步骤当然是让对象实现Parcelable化
代码是这样的:
先是对UserPar的Parcelable化
/**
* 比 Serializable的优势是可实现跨进程通信,并且不哟领反射,自身完成封装和解封装节省内存
* Created by zjygzc on 16/7/18.
*/
public class UserPar implements Parcelable{
private String account;
private String password;
private String emailid;
private UserPar(Parcel in){
account = in.readString();
password = in.readString();
emailid = in.readString();
}
//构造方法
public UserPar(String account, String password, String emailid) {
this.account = account;
this.password = password;
this.emailid = emailid;
}
//get/set方法
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
public int describeContents(){
return 0;
}
public static final Creator<UserPar> CREATOR = new Creator<UserPar>() {
@Override
public UserPar createFromParcel(Parcel in) {
return new UserPar(in);
}
@Override
public UserPar[] newArray(int size) {
return new UserPar[size];
}
};
public void writeToParcel(Parcel out, int flags) {
out.writeString(account);
out.writeString(password);
out.writeString(emailid);
}
}
MainActivity代码是这样的:
public class MainActivity extends AppCompatActivity {
private EditText mAccountEt;
private EditText mPasswordEt;
private EditText mEmailEt;
private User user;
private UserPar userPar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAccountEt = (EditText) findViewById(R.id.et_account);
mPasswordEt = (EditText) findViewById(R.id.et_password);
mEmailEt = (EditText) findViewById(R.id.et_email);
}
public void Next (View view){
String account = mAccountEt.getText().toString();
String password = mPasswordEt.getText().toString();
String emailid = mEmailEt.getText().toString();
if(account != null && account.length() > 0 && password != null &&
password.length() > 0 && emailid != null && emailid.length() > 0) {
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
// user = new User(account,password,emailid);
userPar = new UserPar(account,password,emailid);
Bundle bundle = new Bundle();
// bundle.putString("Account", account);
// bundle.putString("Password",password);
// bundle.putString("Emailid",emailid);
//用 Serializable+bundle实现的数据传递
// bundle.putSerializable("user",user);
// intent.putExtra("data",bundle);
//用Parcelable来实现
bundle.putParcelable("userpar",userPar);
intent.putExtra("data",bundle);
startActivity(intent);
}else {
Toast.makeText(MainActivity.this,"Somewhere input wrong",Toast.LENGTH_LONG).show();
}
}
}
接收端的代码是这样的:
public class ResultActivity extends AppCompatActivity {
private TextView mAccount;
private TextView mPassword;
private TextView mEmailid;
// private User user;
private UserPar userPar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
mAccount = (TextView)findViewById(R.id.result_account);
mPassword = (TextView)findViewById(R.id.result_password);
mEmailid = (TextView)findViewById(R.id.result_email);
//用intent方式也可以直接传递多个数值,需要区分每一个String名,这 里初始化三个content来接收数据
// String content_acc = "";
// String content_pss = "";
// String content_eml = "";
//获得intent
Intent intent = getIntent();
//新建一个bundle来获得intent中携带的bundle
Bundle bundle = intent.getBundleExtra("data");
//user对象来取得bundle中序列化的数据名字user必须与manactivity封装时的名字一样,否则无法获得数据
// user = (User)bundle.getSerializable("user");
//userPar对象来提取bundle中的名字叫userPar的对象,强制转为UserPar对象
userPar = bundle.getParcelable("userpar");
//只用intent来专递时单个数据的获取
// content_acc = bundle.getString("Account");
// content_pss = bundle.getString("Password");
// content_eml = bundle.getString("Emailid");
//给textview设定取得的数据
// mAccount.setText(content_acc);
// mPassword.setText(content_pss);
// mEmailid.setText(content_eml);
//给textview设定取得的数据,直接用user对象中的各个属性获得方式,由构造方法定义
// mAccount.setText(user.getAccount());
// mPassword.setText(user.getPassword());
// mEmailid.setText(user.getEmailid());
mAccount.setText(userPar.getAccount());
mPassword.setText(userPar.getPassword());
mEmailid.setText(userPar.getEmailid());
}
}
Git上的源码在这里哈