mainActivity 打開 OtherActivity:
Intent intent =
new Intent(getApplicationContext(), OtherActivity.
class);
startActivity(intent);
startActivity(intent);
mainActivity 給 OtherActivity 傳參數:
Intent intent =
new Intent(getApplicationContext(), OtherActivity.
class);
// 以下二個為OtherActivity傳參數
intent.putExtra("Name", "eboy");
intent.putExtra("Age", 22);
// 也可以使用Bundle來傳參數
Bundle bundle = new Bundle();
bundle.putString("Name1", "eboy1");
bundle.putInt("Age1", 23);
intent.putExtras(bundle);
startActivity(intent);
// 以下二個為OtherActivity傳參數
intent.putExtra("Name", "eboy");
intent.putExtra("Age", 22);
// 也可以使用Bundle來傳參數
Bundle bundle = new Bundle();
bundle.putString("Name1", "eboy1");
bundle.putInt("Age1", 23);
intent.putExtras(bundle);
startActivity(intent);
OtherActivity 接收來自 mainActivity 的參數:
Intent intent = getIntent();
//
用於激活它的意圖對象
String Name = intent.getStringExtra("Name");
int Age = intent.getIntExtra("Age", 0);
Bundle bundle = intent.getExtras();
String Name1 = bundle.getString("Name1");
int Age1 = bundle.getInt("Age1");
TextView textView = (TextView) this.findViewById(R.id.OtherTextView);
textView.setText(Name + " : " + Age + "/" + Name1 + " : " + Age1);
String Name = intent.getStringExtra("Name");
int Age = intent.getIntExtra("Age", 0);
Bundle bundle = intent.getExtras();
String Name1 = bundle.getString("Name1");
int Age1 = bundle.getInt("Age1");
TextView textView = (TextView) this.findViewById(R.id.OtherTextView);
textView.setText(Name + " : " + Age + "/" + Name1 + " : " + Age1);
如果mainActivity 需要 OtherActivity關閉時返回一些值,則可使用 startActivityForResult來打開OtherActivity,具體用法以后用到時再了解。