先记下要点
跳转
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}
});
传递数据
firstactivity发送数据
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
String data = "hello second";
intent.putExtra("extra_data", data);
startActivity(intent);
}
});
secondactivity接收数据
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
返回数据
跳转前的代码
button1.setOnClickListener(new View.OnClickListener() {
@Override
//重写 onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String returnedData = data.getStringExtra("data_return");
Log.d("FirstActivity", returnedData);
}
break;
default:
}
}
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
String data = "hello second";
intent.putExtra("extra_data", data);
//startActivity(intent);
startActivityForResult(intent, 1);
}
});
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
跳转的代码
@Override
public void onBackPressed(){
Intent intent = new Intent();
intent.putExtra("data_return","hello first");
setResult(RESULT_OK, intent);
finish();
}