1,傳一個值的情況:
Main.activity
Pass value:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Main", "123");
startActivityForResult(intent, 0);
Get value from other activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK)
{
Log.d("debug", "result=" + data.getStringExtra("second"));
}
super.onActivityResult(requestCode, resultCode, data);
}
Test.activity
Get value:
Intent intent = getIntent();
final String num = intent.getStringExtra("Main");
Then pass value to main activity
Intent intent = new Intent();
intent.putExtra("second", "This is Second Activity's value!");
setResult(RESULT_OK, intent);
finish();
如果傳多個值的情況使用 Bundle
Pass value:
Home Activity:
Bundle bundle = new Bundle();
*字符、字符串、布爾、字節數組、浮點數等等,都可以傳*/
bundle.putString("Name", "feng88724");
bundle.putBoolean("Ismale", true);
/*把bundle對象assign給Intent*/
intent.putExtras(bundle);
Get value:
Away Activity:
/*獲取Intent中的Bundle對象*/
Bundle bundle = this.getIntent().getExtras();
*獲取Bundle中的數據,注意類型和key*/
String name = bundle.getString("Name");
boolean ismale = bundle.getBoolean("Ismale");