日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

設(shè)置界面開發(fā)Preference?Library數(shù)據(jù)重建機(jī)制詳解_Android

作者:長安皈故里 ? 更新時(shí)間: 2022-12-03 編程語言

一.界面重建后設(shè)置項(xiàng)數(shù)據(jù)如何復(fù)原的?

主要是介紹Preference Library的數(shù)據(jù)恢復(fù)機(jī)制(比如界面銷毀重建)淺析。

Prefernece基類給我們提供了兩個(gè)方法:

onSaveInstanceState():界面銷毀之前提供保存數(shù)據(jù)的時(shí)機(jī);

onRestoreInstanceState():界面銷毀之后提供恢復(fù)數(shù)據(jù)的時(shí)機(jī);

這兩個(gè)方法名和Activity提供的銷毀重建的相關(guān)方法名都是相同的,所以其作用也都是相同的,這里我們以EditTextPreference類作為入口分析下。

1. onSaveInstanceState()保存數(shù)據(jù)

protected Parcelable onSaveInstanceState() {
    final Parcelable superState = super.onSaveInstanceState();
    if (isPersistent()) {
        return superState;
    }
    final SavedState myState = new SavedState(superState);
    myState.mText = getText();
    return myState;
}

首先isPersistent()這個(gè)方法是用來判斷該設(shè)置項(xiàng)數(shù)據(jù)是否保存到默認(rèn)的SharedPreference中,如果保存,那我們就不需要通過onSaveInstanceState()保存數(shù)據(jù)了。

比如數(shù)據(jù)保存到了SP中,當(dāng)界面銷毀重建時(shí),會重新從SP中讀取數(shù)據(jù)渲染到界面上。

接下來看下isPersistent()返回false的情況:

會構(gòu)造一個(gè)SavedState對象,這個(gè)對象的父類就是一個(gè)Parcelable對象,只不過幫助我們封裝了讀寫getText()對應(yīng)的內(nèi)容:

2. onRestoreInstanceState()恢復(fù)數(shù)據(jù)

protected void onRestoreInstanceState(@Nullable Parcelable state) {
    if (state == null || !state.getClass().equals(SavedState.class)) {
        super.onRestoreInstanceState(state);
        return;
    }
    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());
    setText(myState.mText);
}

首先判斷是否為我們上面保存的SavedState類型的Parcelable對象,如果是,則直接進(jìn)行強(qiáng)制轉(zhuǎn)換,并從對象中獲取到銷毀前的mText設(shè)置項(xiàng)的文本數(shù)據(jù)。

二.Preference的銷毀重建方法是如何收到通知的呢?

這里我們就以onSaveInstanceState()保存重建前數(shù)據(jù)的方法,進(jìn)行分析如何接收到分發(fā)通知進(jìn)行調(diào)用的。

EditTextPreference的方法重寫的方法onSaveInstanceState()最終是在Preference的方法dispatchSaveInstanceState進(jìn)行調(diào)用:

繼續(xù)往下走:

public void saveHierarchyState ( @NonNull Bundle container) {
    dispatchSaveInstanceState( container);
}

最終我們查找到方法saveHierarchyState()PreferenceFragmentCompat這個(gè)類進(jìn)行調(diào)用,這個(gè)類其實(shí)就是一個(gè)Fragment對象:

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    final PreferenceScreen preferenceScreen = getPreferenceScreen();
    if (preferenceScreen != null) {
        Bundle container = new Bundle();
        preferenceScreen.saveHierarchyState(container);
        outState.putBundle(PREFERENCES_TAG, container);
    }
}

我們的數(shù)據(jù)會被保存到container這個(gè)Bundle對象中,然后再將這個(gè)Bundle保存到Fragment的用于保存銷毀重建數(shù)據(jù)的Bundle對象中。

到了這里我們就明白了,是由FragmentonSaveInstanceState()方法分發(fā)了界面銷毀重建前保存數(shù)據(jù)的通知,最終調(diào)用到了PreferenceonSaveInstanceState()方法。

同理,onRestoreInstanceState()的分發(fā)通知也是如此,這里就不再過多進(jìn)行分析了。

總結(jié)

本篇文章主要是分析了Preference Library的界面銷毀重建,數(shù)據(jù)恢復(fù)的機(jī)制,就是利用了Fragment的保存數(shù)據(jù)和恢復(fù)數(shù)據(jù)的時(shí)機(jī)進(jìn)行分發(fā)通知Preference設(shè)置項(xiàng)執(zhí)行相關(guān)的邏輯,希望本篇文章能對你有所幫助。

原文鏈接:https://juejin.cn/post/7159209932804341767

欄目分類
最近更新