1: from a -- activity to B -- Activity
Intent intent = new Intent();intent.setClass(BundleDemoActivity.this, otherActivity.class);
2: transfer data from a -- activity to B -- Activity
Intent intent = new Intent();intent.setClass(BundleDemoActivity.this, otherActivity.class);Bundle bundle = new Bundle();bundle.putDouble("height", height);bundle.putString("sex", sex);intent.putExtras(bundle);
3: obtain the data in the previous A -- activity from B --- activity:
Bundle bundle = this.getIntent().getExtras();String sex = bundle.getString("sex");double height = bundle.getDouble("height");
4: When B -- activity is returned, data in a-activity exists.
Perform the following operations in a -- ati.pdf:
public void onClick(View arg0) {// TODO Auto-generated method stubet = (EditText) findViewById(R.id.height);double height = Double.parseDouble(et.getText().toString());String sex = "";rb1 = (RadioButton) findViewById(R.id.sex1);rb2 = (RadioButton) findViewById(R.id.sex2);if (rb1.isChecked()) {sex = "M";} else {sex = "F";}Intent intent = new Intent();intent.setClass(BundleDemoActivity.this, otherActivity.class);Bundle bundle = new Bundle();bundle.putDouble("height", height);bundle.putString("sex", sex);intent.putExtras(bundle);startActivityForResult(intent, 0);}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch(resultCode){case RESULT_OK:Bundle bundle = data.getExtras();String sex = bundle.getString("sex");double height = bundle.getDouble("height");et.setText(""+height);if(sex.equals("M")){rb1.setChecked(true);}else{rb2.setChecked(true);}break;default:break;}}
At the same time, you must also perform operations in B -- activity:
button =(Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubotherActivity.this.setResult(RESULT_OK, getIntent());otherActivity.this.finish();}});