startActivity V.S. startActivityForResult
將想叫起的Activity變成Intent然後丟給startActivity,讓他去告訴Android我有個意圖,請他執行,並可以透過finish()關掉一個Activity。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Intent serverIntent = new Intent(MainActivity.this, NextActivity.class); | |
startActivity(serverIntent); |
例如:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// button按下後,開啟NextActivity | |
button.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View v) { | |
Intent serverIntent = new Intent(MainActivity.this, NextActivity.class); | |
startActivity(serverIntent); | |
} | |
}); |
startActivity是個單向開啟的動作,可以透過Bundle傳資料給下一個Activity,而下一個Activity可以透過Intent收到資料, 但是原本的Activity沒辦法從下一個Activity那邊接收訊息
例如:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* button按下後,開啟NextActivity | |
* 並透過Bundle將值傳至NextActivity | |
*/ | |
button.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View v) { | |
Intent serverIntent = new Intent(MainActivity.this, NextActivity.class); | |
Bundle bundle = new Bundle(); | |
bundle.putInt("count", 3); //將型態為Int之變數"count"的值設為3 | |
bundle.putBoolean("open", true); //將型態為Boolean之變數"open"的值設為true | |
bundle.putString("name", "ABC"); //將型態為String之變數"name"的值設為ABC | |
serverIntent.putExtras(bundle); | |
//開始呼叫目標Activtiy並傳值 | |
startActivity(serverIntent); | |
} | |
}); |
如果希望可以從新的Activity得到一些資訊就需使用startActivityForResult,他代表我開啟一個Activity並等待他傳些東西回來。
而使用startActivityForResult的時候,必須複寫Activity的onActivityResult函式才能真的有作用。
也需要有一個requestCode參數:為了讓接收資料的onActivityResult能夠辨別是哪個Activity回傳的資料。(因為有可能一個Activity能夠開啟很多不同的Activity)
例如:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final int RQ_CODE = 1; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
/** | |
* button按下後,開啟NextActivity | |
* 並透過Bundle將值傳至NextActivity | |
* 等待NextActivity傳值回來 | |
*/ | |
button = (Button) findViewById(R.id.btn); | |
button.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View v) { | |
Intent serverIntent = new Intent(MainActivity.this, NextActivity.class); | |
Bundle bundle = new Bundle(); | |
bundle.putInt("count", 3); //將型態為Int之變數"count"的值設為3 | |
bundle.putBoolean("open", true); //將型態為Boolean之變數"open"的值設為true | |
bundle.putString("name", "ABC"); //將型態為String之變數"name"的值設為ABC | |
serverIntent.putExtras(bundle); | |
//開始呼叫目標Activtiy並傳值,並附帶requestCode(這裡RQ_CODE設為1)-辨別是呼叫哪個Activity | |
startActivityForResult(serverIntent, RQ_CODE); | |
} | |
}); | |
} | |
//呼叫 onActivityResult 方法 | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
// requestCode = 對應上面的startActivityForResult的第二個參數 | |
if (requestCode == 1) { | |
// resultCode = 對應NextActivity中setResult()的第一個參數 | |
if (resultCode == RESULT_OK) { | |
// 將NextActivity回傳的資料取出來 | |
Bundle bundleResult = data.getExtras(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_next); | |
//取得MainActivity傳的值 | |
Bundle bundle = this.getIntent().getExtras(); | |
int Count = bundle.getInt("count"); | |
boolean Open = bundle.getBoolean("open"); | |
String Name = bundle.getString("name"); | |
//將值回傳給MainActivity | |
String resultValue = "count=" + Count; | |
Intent resultIntent = new Intent(); //創建一個Intent | |
Bundle bundleBack = new Bundle(); //創建一個Bundle,傳值之用 | |
bundleBack.putString("resultValue", resultValue); | |
resultIntent.putExtras(bundleBack); | |
setResult(RESULT_OK, resultIntent); //回傳給MainActivity,RESULT_OK為回傳狀態 | |
//回傳狀態包括RESULT_OK、RESULT_CANCELED、RESULT_FIRST_USER | |
finish(); | |
} |
參考來源:東方和風語
留言
張貼留言