public class OnclickActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
textView = (TextView) findViewById(R.id.one);
}
public void one(View v) {
Intent intent = new Intent(OnclickActivity.this, ActivityTwo.class);
String passString = "Hello,this is A";
intent.putExtra("ToB", passString);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle b = data.getExtras(); // data为B中回传的Intent
String str = b.getString("FromB");// str即为回传的值
textView.setText(str);
}
}
}
第二个Activity
public class ActivityTwo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
TextView textView=(TextView) findViewById(R.id.two);
String result = getIntent().getStringExtra("ToB");
textView.setText(result);
}
public void two(View v) {
Intent intent = new Intent();
String passString = "Hello,this is B";
intent.putExtra("FromB", passString);
setResult(RESULT_OK, intent);
finish();
}
}
public class OnclickActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
textView = (TextView) findViewById(R.id.one);
}
public void one(View v) {
Intent intent = new Intent(OnclickActivity.this, ActivityTwo.class);
String passString = "Hello,this is A";
intent.putExtra("ToB", passString);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle b = data.getExtras(); // data为B中回传的Intent
String str = b.getString("FromB");// str即为回传的值
textView.setText(str);
}
}
}
第二个Activity
public class ActivityTwo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
TextView textView=(TextView) findViewById(R.id.two);
String result = getIntent().getStringExtra("ToB");
textView.setText(result);
}
public void two(View v) {
Intent intent = new Intent();
String passString = "Hello,this is B";
intent.putExtra("FromB", passString);
setResult(RESULT_OK, intent);
finish();
}
}