第一个界面
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//第二个参数是请求码
startActivityForResult(intent,1);
}
});
//requestCode是判断数据来源,resultCode是判断处理结果是否成功
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK){
String data1 = data.getStringExtra("data");
Toast.makeText(this, ""+data1, Toast.LENGTH_SHORT).show();
Log.e("MainActivity---", "onActivityResult: "+data1 );
}
}
}
第二个界面
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data","Hello");
setResult(RESULT_OK,intent);
finish();
}
});
网友评论