編輯:關於 Android 程式設計
上一講留了一個問題:Activity 執行 onCreateView 時,Fragment 裡的那個 View 參數到底是怎麼構造出來的?要回答這個問題,必須先理解 Fragment 的狀態——這是整個 Fragment 管理機制中最關鍵的一環。
先看 FragmentActivity 提供的核心回調:
@Override
protected void onCreate(Bundle savedInstanceState) {
mFragments.attachActivity(this, mContainer, null);
// 舊版本平台不會自己做這件事
if (getLayoutInflater().getFactory() == null) {
getLayoutInflater().setFactory(this);
}
super.onCreate(savedInstanceState);
...
mFragments.dispatchCreate();
}
跟進 dispatchCreate():
public void dispatchCreate() {
mStateSaved = false;
moveToState(Fragment.CREATED, false);
}
可以看到,對 FragmentManager 來說,這裡做了一次狀態轉換。
FragmentManager 是極其重要的類,承擔了 Fragment 管理最核心的工作。它內部維護著自己的狀態機,而這個狀態可以理解為與 Activity 本身基本同步。
也就是說,當你往 FragmentManager 裡添加一個 Fragment,它的目標就是讓這個 Fragment 的狀態與自己保持一致。
void moveToState(int newState, int transit, int transitStyle, boolean always) {
if (mActivity == null && newState != Fragment.INITIALIZING) {
throw new IllegalStateException("No activity");
}
if (!always && mCurState == newState) {
return;
}
mCurState = newState;
if (mActive != null) {
boolean loadersRunning = false;
for (int i = 0; i < mActive.size(); i++) {
Fragment f = mActive.get(i);
if (f != null) {
moveToState(f, newState, transit, transitStyle, false);
if (f.mLoaderManager != null) {
loadersRunning |= f.mLoaderManager.hasRunningLoaders();
}
}
}
...
}
}
結論很清晰:FragmentManager 的每一次狀態變更,都會引起 mActive 中所有 Fragment 的狀態變更。而 mActive 正是所有納入 FragmentManager 管理的 Fragment 容器。
static final int INITIALIZING = 0; // 尚未創建 static final int CREATED = 1; // 已創建 static final int ACTIVITY_CREATED = 2; // Activity 已完成了它的創建 static final int STOPPED = 3; // 完全創建,尚未啟動 static final int STARTED = 4; // 已創建並啟動,尚未 resume static final int RESUMED = 5; // 已創建、已啟動、已 resume
狀態越靠後,數值越大。FragmentManager 在管理中巧妙地利用了這一點:
if (f.mState < newState) {
...
} else {
...
}
f.mState < newState 可以理解為「向上創建」的過程,反之則是「向下銷燬」。
當 Fragment 是伴隨佈局一起加載的(f.mFromLayout == true),View 必須立刻創建,因為 LayoutInflater 需要馬上把它加到父容器裡:
if (f.mFromLayout) {
// 對於作為內容視圖佈局一部分的 Fragment,
// 需要立即實例化 View,後續由 inflater 負責添加
f.mView = f.performCreateView(
f.getLayoutInflater(f.mSavedFragmentState), null,
f.mSavedFragmentState);
if (f.mView != null) {
f.mInnerView = f.mView;
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
if (f.mHidden) {
f.mView.setVisibility(View.GONE);
}
f.onViewCreated(f.mView, f.mSavedFragmentState);
} else {
f.mInnerView = null;
}
}
f.mFromLayout 表示這個 Fragment 是否從 layout.xml 中生成。而 View 的構造,正是通過 performCreateView:
View performCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mChildFragmentManager != null) {
mChildFragmentManager.noteStateNotSaved();
}
return onCreateView(inflater, container, savedInstanceState);
}
沒錯,這就是我們最熟悉的 onCreateView 回調的真正出處。
此時 Fragment 自身仍處於 Fragment.INITIALIZING 狀態,但 FragmentManager 已經進入 CREATED。所以 newState 應該是 CREATED,代碼沿著 case 分支繼續往下走:
case Fragment.CREATED:
if (newState > Fragment.CREATED) {
if (!f.mFromLayout) {
ViewGroup container = null;
if (f.mContainerId != 0) {
container = (ViewGroup) mContainer.findViewById(f.mContainerId);
if (container == null && !f.mRestored) {
throwException(new IllegalArgumentException(
"No view found for id 0x"
+ Integer.toHexString(f.mContainerId)
+ " (" + f.getResources()
.getResourceName(f.mContainerId)
+ ") for fragment " + f));
}
}
f.mContainer = container;
f.mView = f.performCreateView(
f.getLayoutInflater(f.mSavedFragmentState),
container, f.mSavedFragmentState);
if (f.mView != null) {
f.mInnerView = f.mView;
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
if (container != null) {
Animation anim = loadAnimation(f, transit, true, transitionStyle);
if (anim != null) {
f.mView.startAnimation(anim);
}
container.addView(f.mView);
}
if (f.mHidden) {
f.mView.setVisibility(View.GONE);
}
f.onViewCreated(f.mView, f.mSavedFragmentState);
} else {
f.mInnerView = null;
}
}
f.performActivityCreated(f.mSavedFragmentState);
if (f.mView != null) {
f.restoreViewState(f.mSavedFragmentState);
}
f.mSavedFragmentState = null;
}
這段邏輯處理的是「FragmentManager 狀態在 CREATED 以上,且 Fragment 並非通過 layout.xml 導入」的情況。原因也在情理之中:Activity 的 onCreate 執行完畢後,你的控件基本已經就位,你要做的只是在生成的控件層級中找到 Fragment 對應的容器,然後把 Fragment 的 View 裝進去。
順帶注意這裡的動畫處理:
if (container != null) {
Animation anim = loadAnimation(f, transit, true, transitionStyle);
if (anim != null) {
f.mView.startAnimation(anim);
}
container.addView(f.mView);
}
動畫的加載依賴 transit 和 transitionStyle 兩個參數,它們的來源和配置方式,留到後面講 Fragment 事務時再細說。
小結一下:Fragment 的 View 何時創建,取決於它是「伴隨佈局加載」還是「代碼動態添加」。前者在 INITIALIZING 階段就急著把 View 造出來交給 inflater;後者等到 FragmentManager 進入 CREATED 之後,找到容器再裝入。狀態機的數值大小比較,正是這套分流機制的基礎。
仿微信通訊錄:右側字母表快速定位控件的實現
很多應用的通訊錄界面(如 MIUI 通訊錄)都有右側字母快速定位功能,這其實是一個自定義 View,並不算特別複雜。 先看效果圖: 下面介紹實現思路。自定義 View
Android Launcher 源碼研究:載入應用資料並綁定畫面
Launcher 在啟動時會透過背景執行緒載入應用資訊、桌面捷徑與小工具,再將資料綁定到畫面上。這個過程通常稱為 bind。 啟動載入流程 Launcher 會在 on
四大組件之 ContentProvider(一):Uri 設計與使用系統內建的內容提供者
一、ContentProvider 是什麼 ContentProvider 是 Android 四大組件之一,專責對外提供資料存取能力。可以把它想成一個小型網站:其他組件
為你的 Android App 實現自簽名 SSL 憑證(HTTPS)
為何需要 HTTPS 一般網路請求多採用 https://,透過以下程式碼開啟連線: URL url = new URL(https://www.codeproject.