ActivityのIntentコントロールの使い方

1440 ワード

一、パラメータ付きの異なるActivity間のデータ伝達の最初のActivity:
/*new Intent , class*/
        Intent intent = new Intent(); 
        intent.setClass(EX03_10.this,EX03_10_1.class); 
        /*new Bundle , */
        Bundle bundle = new Bundle();
        bundle.putDouble("height",height);
        bundle.putString("sex",sex); 
        /* Bundle assign Intent*/ 
        intent.putExtras(bundle); 
        /* Activity EX03_10_1*/ 
        startActivity(intent); 

2番目のActivity:
/*  Intent Bundle  */
    Bundle bunde = this.getIntent().getExtras();
    /*  Bundle  */
    String sex = bunde.getString("sex");
    double height = bunde.getDouble("height");

二、前のActivityの最初のActivityにデータを返す.
Intent intent = new Intent();
intent.setClass(EX03_11.this, EX03_11_1.class); 
startActivityForResult(intent, 0);
/*   onActivityResult() */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case RESULT_OK:
break;
default:
break;
}
}
 
2番目のActivity:
Intent intent = this.getIntent();
EX03_11_1.this.setResult(RESULT_OK, intent); /*  activity */
EX03_11_1.this.finish();