使用Intent在不同activity間進(jìn)行傳參的兩種方法
- 使用Intent的putExtra()方法進(jìn)行參數(shù)傳遞
傳遞參數(shù)
Intent intent = new Intent(FromActivity.this, ToActivity.class);
/*設(shè)置inent的傳遞參數(shù)*/
intent.putExtra("args", args);
startActivity(intent);
接收參數(shù)
// 接收傳入的intent對(duì)象
Intent intent = getIntent();
if(intent != null){
//獲取intent中的參數(shù)
String regAccount = intent.getStringExtra("args");
}
- 使用Bundle對(duì)象將參數(shù)封裝進(jìn)行傳遞
封裝并傳遞參數(shù)
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
// 使用Bundle對(duì)象進(jìn)行傳參,做一層封裝
Bundle bundle = new Bundle();
bundle.putString("accountName",accountName);
bundle.putInt("accountAge",accountAge);
// intent對(duì)象綁定bundle對(duì)象
intent.putExtras(bundle);
startActivity(intent);
接收參數(shù)
// 接受傳入的intent對(duì)象
Intent intent = getIntent();
if(intent != null){
// 獲取Bundle對(duì)象中的參數(shù)
Bundle bundle = intent.getExtras();
if(bundle != null){
String accountName = bundle.getString("accountName", "");
Int accountAge = bundle.getInt("accountAge", 1);
}