2/16/2014

[Android] 開啟另一個Activity


開啟另一個Activity ( A 呼 B
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);

不同 Activity 之間傳值

A.class
Intent intent = new Intent();
intent.setClass(this, Report.class);
Bundle bundle = new Bundle();
bundle.putString("VAR_A", field_height.getText().toString());
bundle.putString("VAR_B", field_weight.getText().toString());
intent.putExtras(bundle);
startActivity(intent);

B.class
Bundle bundle = this.getIntent().getExtras();
double a= Double.parseDouble(bundle.getString("VAR_A"));
double b= Double.parseDouble(bundle.getString("VAR_B"));


回傳資料給前一個 Activity 並回復其狀態
A.class
//呼叫B
startActivityForResult(intent);

//接受B的回傳值
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
             Bundle bundleResult = data.getExtras();
        }
}

B.class
//回傳bundle 給前一個 activity
B.this.setResult(RESULT_OK, intent);
B.this.finish();


不重複啟動相同的Activity
Manifest.xml配置成android:launchMode="singleTask"

沒有留言:

張貼留言