android传值

需求

OneActivity向TwoActivity传值name=hzs,然后TwoActivity向OneActivity传值sex=Y

第一步:OneActivity向TwoActivity传值name=hzs

OneActivity 里设置

Intent intent = new Intent(OneActivity.this,TwoActivity.class);
intent.putExtra("name","hzs");
startActivityForResult(intent,2008); //code是请求码 int类型

TwoActivity 里取值

Intent getIntent = getIntent();
String name = getIntent.getStringExtra("name");

第二步:TwoActivity向OneActivity传值sex=Y

TwoActivity 里设置

Intent intent = new Intent();
intent.putExtra("sex", "Y");
setResult(1, intent); //resultCode 为返回码 用于区分从哪里返回。
finish();

OneActivity 里取值

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch(requestCode){

case 2000:

if(resultCode==1){

String sex = data.getStringExtra("sex");

}

break;

default:

}

super.onActivityResult(requestCode, resultCode, data); 
}

上一篇:python之进程,线程,协程简单理解


下一篇:为什么 APM 能提升 IT 团队工作质量?