Android之activity反传参数

1.前一个activity用startActivityForResult()跳转

private static final int REQUEST_CODE = 1;

Intent intent = new Intent(this,DetailActivity.class);

startActivityForResult(intent,REQUEST_CODE);

2.重写后一个activity的finish()方法

@Override
public void finish(){
String name = this.editText_age.getText().toString().trim();
String age = this.editText_name.getText().toString().trim();

Intent data = new Intent();
data.putExtra("name",name);
data.putExtra("age",Integer.parseInt(age));
//同上
this.setResult(RESULT_OK,data);
//销毁当前Activity必须放到最后
super.finish();
}

3.获取反转的值

protected void onActivityResult(int requestCode,int resultCode,Intent data){
//
if(requestCode==REQUEST_CODE){
if(requestCode==RESULT_OK){
String name = data.getStringExtra("name");
int age = data.getIntExtra("age",0);
System.out.println("onActivityResult().name="+name+",age="+age);
}
}
System.out.println("======MainActivity.onActivityResult(int requestCode="+requestCode+", int resultCode="+resultCode+", Intent data="+data+")======");
}

补:前一个activity完整代码

public class MainActivity extends AppCompatActivity {
/**
* 自定义请求码常量
*/
private static final int REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* @parm view
*/
public void openDetail(View view ){
Intent intent = new Intent(this,DetailActivity.class);
startActivityForResult(intent,REQUEST_CODE);
}
//
protected void onActivityResult(int requestCode,int resultCode,Intent data){
//
if(requestCode==REQUEST_CODE){
if(requestCode==RESULT_OK){
String name = data.getStringExtra("name");
int age = data.getIntExtra("age",0);
System.out.println("onActivityResult().name="+name+",age="+age);
}
}
System.out.println("======MainActivity.onActivityResult(int requestCode="+requestCode+", int resultCode="+resultCode+", Intent data="+data+")======");
}
}

Android之activity反传参数

上一篇:jackson学习之四:WRAP_ROOT_VALUE(root对象)


下一篇:APP性能测试