編輯:Android 開發實例
在多 Activity 的專案中,不管是自家應用內部的跳轉,還是引用其他應用提供的可重用 Activity,都可能遇到同一個問題:每次跳轉都產生一個新實例,於是堆疊不斷膨脹、按返回鍵要按到手軟。
要解決這件事,就必須為 Activity 指定合適的啟動模式(launchMode),而不是一律沿用預設值。
Activity 共有四種啟動模式:
設定方式是修改 AndroidManifest.xml 中 activity 元素的 android:launchMode 屬性:
<activity android:name="ActB"
android:launchMode="singleTask"></activity>
在 Eclipse ADT 中也可以直接透過圖形介面編輯,效果相同。
下面用一組 ActA 與 ActB 互相跳轉的範例,實際觀察四種模式的差異。範例刻意不使用版面配置檔,改以程式碼動態組出 TextView 與 Button,並把物件的 toString(含 hash code)印在畫面上,方便判斷是否真的產生了新實例。
standard 是預設模式,不需要額外設定。以下 ActA 的按鈕會再次啟動自己:
package com.easymorse.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ActA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText(this + "");
Button button = new Button(this);
button.setText("go actA");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ActA.this, ActA.class);
startActivity(intent);
}
});
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(button);
this.setContentView(layout);
}
}
每按一次按鈕,畫面上的 hash code 就換一組,代表每次都建立了新實例。此時按返回鍵,會依照建立的相反順序逐一退回——跳轉是壓棧,返回是退棧。
singleTop 與 standard 一樣會把 Intent 導向實例,但有個關鍵前提:若棧頂已經存在要建立的 Activity 實例,則把 Intent 交給該實例,不再新建。
只要把上面的 launchMode 改成 singleTop,就會發現無論按幾次按鈕,hash code 都沒有變化——因為該實例一直在棧頂。此時按返回鍵,就是直接離開應用。
所以 singleTop 最適合用來避免「同一個 Activity 在棧頂層層堆疊」。不過要注意它的限制:如果是 A 跳到 B、B 再跳回 A,當 B 啟動 A 時棧頂並不是 A,於是仍會建立 A 的新實例,行為與 standard 無異。
把 ActA 改成跳轉到 ActB:
package com.easymorse.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ActA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText(this + "");
Button button = new Button(this);
button.setText("go actB");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ActA.this, ActB.class);
startActivity(intent);
}
});
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(button);
this.setContentView(layout);
}
}
ActB 則保持預設的 standard,按鈕負責跳回 ActA:
package com.easymorse.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class ActB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("go actA");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ActB.this, ActA.class);
startActivity(intent);
}
});
LinearLayout layout = new LinearLayout(this);
layout.addView(button);
this.setContentView(layout);
}
}
在這個 A→B→A 的循環中,即使 ActA 設為 singleTop,從 ActB 跳回時仍會產生新實例;把 ActA 改回 standard,觀察到的現象完全一樣。
singleTask 與 singleInstance 都保證同一個 Activity 只會有一個實例。差別在範圍:singleTask 是「同一個 Task 內唯一」。
當 Intent 送達而需要建立 singleTask 的 Activity 時,系統會先檢查堆疊中是否已存在該 Activity 的實例,若有就直接把 Intent 交給它,並清掉它上方的其他 Activity。
把範例中 ActA 改為 singleTask、ActB 維持 standard,從 ActA 按一次按鈕進入 ActB,再按返回——你會發現回到的是原本那個 ActA 實例(hash code 沒變)。無論來回切換多少次,ActA 永遠只有一個實例存在。
要理解 singleInstance,得先談 Task(任務)的概念。
在桌面系統中,即使應用有多個視窗,你也無法直接重用別人的視窗——除非對方提供了 API。Android 則允許「讓其他應用直接重用你的 Activity」,而支撐這套機制的正是 Task。
Task 可以理解為一個堆疊,可容納多個 Activity。當你啟動一個應用,Android 便建立一個 Task,並放入該應用的入口 Activity(intent-filter 中 MAIN + LAUNCHER 那個)。這個 Activity 就是根(Root)Activity,後續在它上面開啟的其他 Activity,若採用前述三種模式,也會留在同一個 Task 中,只是實例化策略不同。
驗證方式很簡單,印出 taskId 即可:
TextView textView2 = new TextView(this);
textView2.setText("task id: " + this.getTaskId());
你會發現,無論如何切換 Activity,taskId 都維持不變。
Task 的價值在於使用者體驗的連貫:例如在地圖 Activity 看完路線按返回,會退回原本呼叫地圖的畫面,使用者完全不覺得自己在多個應用之間跳躍。
但接著會出現另一種需求:多個 Task 共享同一個 Activity。singleTask 是「一個 Task 內共享一個實例」,而 singleInstance 才是「跨多個 Task 共享一個實例」。
最典型的例子就是系統地圖。假設你開發的是導覽應用,其中叫起地圖 Activity;此時按 Home 鍵,再從應用列表打開地圖,你看到的仍是剛才那一頁——因為那是同一個 Activity 實例。
若採用前三種模式,這個需求無法實現。而且現實情況更複雜:地圖應用自己也有路線規劃等上下文 Activity,導覽應用也有自己的上下文;各自按返回都應該回到各自的上下文。singleInstance 的解法是把這個 Activity 獨立放在一個只屬於它自己的 Task 中,兩個應用送出的 Intent 都由這唯一的實例接收與呈現。
回到範例:把 ActB 設為 singleInstance、ActA 設為 standard,從 ActA 切換到 ActB,會看到兩者分屬不同的 Task;此時印出 taskId,兩者也的確不同。
Android 多語系應用:字串資源與區域資料夾
多語系應用的基本概念Android 會依裝置語言與地區選擇適合的資源。若應用只提供預設字串,在非預設語言環境下可能會顯示不夠在地化的內容。建立 values-it 等資源
第三方應用接入微信平台研究分享(一):SDK 結構與通信流程
微信平台開放之後,不少第三方應用都想接進來——畢竟能借用微信建立起來的人際鏈來擴展自己的應用,確實比自己從零經營社交省下可觀的成本。由於實習需要,我最近也在研究這塊,不過
Android JSON 解析入門:從天氣 API 擷取資料
JSON 是應用程式之間常見的資料交換格式,因為結構清楚、易於閱讀,也方便在 Android 中處理。當 App 需要從網路取得資料時,通常會先取得 JSON 字串,再解
Android TextView 控件詳解:屬性、方法與使用要點
文本控件的兩大主力 在 Android 開發中,凡是需要與「文字」打交道的地方,都離不開兩個控件:TextView 與 EditText。前者負責展示,後者負責收集輸入。