編輯:Android 開發實例
Activity 跳轉大家都知道用 startActivity(Intent)。但如果情境是:Activity1 跳到 Activity2,處理完之後還要把結果帶回 Activity1 呢?
有人會說,那在 Activity2 再 startActivity 一次不就得了?可以,但 startActivityForResult() 能更直接地完成這件事。
示例:Activity1 有兩個 EditText 用來接收使用者輸入的字串,要求把兩個字串串接起來。串接的工作交給 Activity2 做,做完再把結果回傳給 Activity1 顯示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/second"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...is waiting" />
</LinearLayout>
first = (EditText) findViewById(R.id.first); second = (EditText) findViewById(R.id.second);
Intent i = new Intent(this, Activity2.class);
Bundle b = new Bundle();
b.putString("first", first.getText().toString());
b.putString("second", second.getText().toString());
i.putExtras(b);
startActivityForResult(i, 10);
方法簽名值得細看:
public void startActivityForResult(Intent intent, int requestCode)
intent:系統依此決定要啟動哪個 Activity。
requestCode:這次請求的識別碼。回來時靠它判斷「這個結果是不是我當初要的那一個」——當一個 Activity 裡有多處 start 動機時尤其重要。
findViewById(R.id.start).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendCalculate();
}
});
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/reply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="reply" />
</LinearLayout>
Intent i = this.getIntent();
Bundle b = i.getExtras();
String v1 = b.getString("first");
String v2 = b.getString("second");
value = v1 + v2;
注意這裡用的是 getIntent() 而不是 getCallingIntent()——兩者差別在於前者對任何啟動方式都有效,後者只在 forResult 情境下才有值。
Intent i = new Intent();
Bundle b = new Bundle();
b.putString("CALCULATION", value);
i.putExtras(b);
this.setResult(RESULT_OK, i);
this.finish();
setResult() 的第二個參數是回傳的 Intent;第一個參數是結果碼,常用值為 RESULT_OK、RESULT_CANCELED。若不呼叫 setResult,系統會預設回傳 RESULT_CANCELED,這是很多人「明明 finish 了卻收不到結果」的原因。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case RESULT_OK:
Bundle b = data.getExtras();
String string = b.getString("CALCULATION");
updateText(string);
break;
}
}
實務上建議同時檢查 requestCode,避免多個請求的結果互相干擾:
if (requestCode == 10 && resultCode == RESULT_OK) {
String result = data.getStringExtra("CALCULATION");
updateText(result);
}
另外提醒:使用者若按返回鍵離開 Activity2,resultCode 會是 RESULT_CANCELED,而 data 可能為 null。取值前應先判空,否則容易 crashes。
上述是傳統做法。若你在新專案中實作類似需求,AndroidX 已提供 Activity Result API,不需要自己記 requestCode,也不用手動處理 onActivityResult:
private final ActivityResultLauncher<Intent> launcher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
String s = result.getData().getStringExtra("CALCULATION");
updateText(s);
}
});
// 啟動
launcher.launch(new Intent(this, Activity2.class));
它天生就處理了設定變更與 requestCode 分派,也不會因為混淆而取錯結果。理解 startActivityForResult 的運作有助於除錯舊專案,但新程式碼請優先採用 Activity Result API。
Android 本地化實作:依語言環境切換字串資源
當 App 要面對不同語言的市場時,本地化是必要步驟。Android 透過資源資料夾的命名規則,讓系統自動選擇最適合的語言資源。 字串資源 預設字串放在 res/valu
Android 登入畫面實作教學:帳密驗證與三次錯誤鎖定
登入畫面是許多 App 的第一道門。使用者可能要先登入 Facebook、微博或其他服務才能使用完整功能。本章從版面出發,依序完成輸入欄位、帳密比對,以及防止連續猜錯的保
Android 登入畫面實作:帳密驗證與錯誤次數限制
幾乎每個需要使用者帳號的 App 都少不了一個登入畫面,無論是自家服務還是 Facebook、微博這類第三方平台。本章要說明如何打造一個登入介面,並處理最容易被忽略的兩件
仿 QQ 空間底部中央加號選單:Android 完整實作解析
網路上搜得到的「仿 QQ 空間底部選單」範例不少,但仔細看會發現很多問題:布局裡塞滿了寫死的魔法數字、不同解析度下位置就歪掉、PopupWindow 的生命週期也沒處理好