Jetpack(三):ViewModel學(xué)習(xí)記錄

原理

MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class)

由于在Activity中調(diào)用耗啦,所以this值為Activity套菜,在Fragment中赐写,this則為Fragment,因此of肯定有多個(gè)構(gòu)造方法,以Activity中為例

源碼

ViewModelProviders.java的of

@NonNull
    @MainThread
    public static ViewModelProvider of(@NonNull FragmentActivity activity) {
        return of(activity, null);
    }

@NonNull
    @MainThread
    public static ViewModelProvider of(@NonNull Fragment fragment, @Nullable Factory factory) {
        Application application = checkApplication(checkActivity(fragment));
        if (factory == null) {
            factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
        }
        return new ViewModelProvider(fragment.getViewModelStore(), factory);
    }

第2個(gè)of函數(shù)里,會(huì)調(diào)用getApplication方法來返回Activity對應(yīng)的Application

if語句中會(huì)創(chuàng)建AndroidViewModelFactory實(shí)例。最后會(huì)新建一個(gè)ViewModelProvider,將AndroidViewModelFactory作為參數(shù)傳入


ViewModelProvider.java的AndroidViewModelFactory

public static class AndroidViewModelFactory extends ViewModelProvider.NewInstanceFactory {

        private static AndroidViewModelFactory sInstance;

        /**
         * Retrieve a singleton instance of AndroidViewModelFactory.
         *
         * @param application an application to pass in {@link AndroidViewModel}
         * @return A valid {@link AndroidViewModelFactory}
         */
        @NonNull
        public static AndroidViewModelFactory getInstance(@NonNull Application application) {
            if (sInstance == null) {
                sInstance = new AndroidViewModelFactory(application);
            }
            return sInstance;
        }

        private Application mApplication;

        /**
         * Creates a {@code AndroidViewModelFactory}
         *
         * @param application an application to pass in {@link AndroidViewModel}
         */
        public AndroidViewModelFactory(@NonNull Application application) {
            mApplication = application;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            if (AndroidViewModel.class.isAssignableFrom(modelClass)) {
                //noinspection TryWithIdenticalCatches
                try {
                    return modelClass.getConstructor(Application.class).newInstance(mApplication);
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, e);
                } catch (InstantiationException e) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, e);
                }
            }
            return super.create(modelClass);
        }
    }

可以看到塔嬉,AndroidViewModelFactory是一個(gè)單例,ViewModel本身是一個(gè)抽象類租悄,我們一般通過繼承ViewModel來實(shí)現(xiàn)自定義ViewModel谨究,那么AndroidViewModelFactory的create方法的作用就是通過反射生成ViewModel的實(shí)現(xiàn)類的

再來看看ViewModelProvider的get方法

ViewModelProvider.java的get

@NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
        String canonicalName = modelClass.getCanonicalName();
        if (canonicalName == null) {
            throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
        }
        return get(DEFAULT_KEY + ":" + canonicalName, modelClass);
    }


@NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
        ViewModel viewModel = mViewModelStore.get(key);

        if (modelClass.isInstance(viewModel)) {
            //noinspection unchecked
            return (T) viewModel;
        } else {
            //noinspection StatementWithEmptyBody
            if (viewModel != null) {
                // TODO: log a warning.
            }
        }

        viewModel = mFactory.create(modelClass);
        mViewModelStore.put(key, viewModel);
        //noinspection unchecked
        return (T) viewModel;
    }

第1個(gè)get中,先拿到modelClass的類名泣棋,并對其進(jìn)行字符串拼接胶哲,作為第2個(gè)get的參數(shù),DEFAULT_KEY就是androidx.lifecycle.ViewModelProvider.DefaultKey

因此潭辈,第2個(gè)get方法中拿到的key就是DEFAULT_KEY + 類名鸯屿,根據(jù)key從ViewModelStore獲取ViewModel的實(shí)現(xiàn)類。如果ViewModel能直接轉(zhuǎn)成modelClass類的對象把敢,則直接返回該ViewModel寄摆,否則會(huì)通過Factory創(chuàng)建一個(gè)ViewModel,并將其存儲到ViewModelStore中修赞。這里的Factory指的就是AndroidViewModelFactory婶恼,在上面我們提到的ViewModelProvider創(chuàng)建時(shí)作為參數(shù)被傳進(jìn)來


關(guān)系解析

ViewModelProvider.java:為Fragment,Activity等提供ViewModels的utils類

ViewModelProviders.java:提供of方法榔组,返回一個(gè)ViewModelProvider

ViewModelStore.java:以HashMap形式緩存ViewModel熙尉,需要時(shí)從緩存中找,有則返回搓扯,無則創(chuàng)建

ViewModelStores.java:提供of方法,返回一個(gè)ViewModelStore給需要的Activity或Fragment

AndroidViewModelFactory:ViewModelProvider的靜態(tài)內(nèi)部類包归,提供create方法反射創(chuàng)建ViewModel實(shí)現(xiàn)類


如何在旋轉(zhuǎn)后保存數(shù)據(jù)

/**
     * Retain all appropriate fragment state.  You can NOT
     * override this yourself!  Use {@link #onRetainCustomNonConfigurationInstance()}
     * if you want to retain your own state.
     */
@Override
    public final Object onRetainNonConfigurationInstance() {
        Object custom = onRetainCustomNonConfigurationInstance();

        FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();

        if (fragments == null && mViewModelStore == null && custom == null) {
            return null;
        }

        NonConfigurationInstances nci = new NonConfigurationInstances();
        nci.custom = custom;
        nci.viewModelStore = mViewModelStore;
        nci.fragments = fragments;
        return nci;
    }


/**
     * Use this instead of {@link #onRetainNonConfigurationInstance()}.
     * Retrieve later with {@link #getLastCustomNonConfigurationInstance()}.
     */
    public Object onRetainCustomNonConfigurationInstance() {
        return null;
    }

    /**
     * Return the value previously returned from
     * {@link #onRetainCustomNonConfigurationInstance()}.
     */
    @SuppressWarnings("deprecation")
    public Object getLastCustomNonConfigurationInstance() {
        NonConfigurationInstances nc = (NonConfigurationInstances)
                getLastNonConfigurationInstance();
        return nc != null ? nc.custom : null;
    }

核心就在這里锨推,我們不可以重寫第一個(gè)方法,如果想要實(shí)現(xiàn)自己的保存數(shù)據(jù)方式需要重寫Custom方法,原生保存數(shù)據(jù)的流程是當(dāng)我們旋轉(zhuǎn)屏幕换可,系統(tǒng)會(huì)調(diào)用onRetainNonConfigurationInstance方法椎椰,在這個(gè)方法內(nèi)會(huì)將我們的ViewModelStore進(jìn)行保存。一旦當(dāng)前activity去獲取ViewModelStore沾鳄,會(huì)通過getLastNonConfigurationInstance方法恢復(fù)之前的ViewModelStore慨飘,所以狀態(tài)改變前后的ViewModelStore是同一個(gè)


為何傳入Context會(huì)內(nèi)存泄漏

ViewModel之所以不應(yīng)該包含Context的實(shí)例或類似于上下文的其他對象的原因是因?yàn)樗哂信cActivity和Fragment不同的生命周期。假設(shè)我們在應(yīng)用上進(jìn)行了更改译荞,這會(huì)導(dǎo)致Activity和Fragment自行銷毀瓤的,因此它會(huì)重新創(chuàng)建。ViewModel意味著在此狀態(tài)期間保持不變吞歼,因此如果它仍然在被破壞的Activity中持有View或Context圈膏,則可能會(huì)發(fā)生崩潰和其他異常


應(yīng)用舉例

MyViewModel

public class MyViewModel extends ViewModel {

    public int num;
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.237" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="AddNum"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

    TextView textView;
    MyViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        //viewmodel中不要傳入context,如果必須要使用篙骡,換成AndroidViewModel里的Application
        viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
        textView.setText(String.valueOf(viewModel.num));
    }

    //保存瞬態(tài)數(shù)據(jù)稽坤,屏幕旋轉(zhuǎn)后數(shù)據(jù)還在
    public void AddNum(View view) {
        textView.setText(String.valueOf(++viewModel.num));
    }
}

這樣就簡單創(chuàng)建了一個(gè)Button和一個(gè)TextView用于顯示數(shù)字,在屏幕旋轉(zhuǎn)后數(shù)字并不會(huì)重置


配合LiveData

MyViewModel

public class MyViewModel extends ViewModel {

    private MutableLiveData<Integer> currentSecond;

    public MutableLiveData<Integer> getCurrentSecond(){
        if(currentSecond == null){
            currentSecond = new MutableLiveData<>();
            currentSecond.setValue(0);
        }
        return currentSecond;
    }
}


activity_data.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DataActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

DataActivity

public class DataActivity extends AppCompatActivity {

    private MyViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        TextView textView = findViewById(R.id.textView);
        viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
        textView.setText(String.valueOf(viewModel.getCurrentSecond().getValue()));

        viewModel.getCurrentSecond().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                textView.setText(String.valueOf(integer));
            }
        });
        startTime();
    }

    private void startTime(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                //非UI線程糯俗,用postValue
                //UI線程尿褪,用setValue
                viewModel.getCurrentSecond().postValue(viewModel.getCurrentSecond().getValue() + 1);
            }
        },1000,1000);
    }

這樣就創(chuàng)建了一個(gè)計(jì)時(shí)器,在屏幕旋轉(zhuǎn)后由于LiveData和ViewModel配合得湘,計(jì)時(shí)器并不會(huì)重置


總結(jié)

ViewModel的大體使用方式在于他調(diào)用了ViewModelProvider構(gòu)造器杖玲,構(gòu)造器中傳入的第一個(gè)參數(shù)是ViewModelStoreOwner,一般在Activity或者Fragment中直接傳this即可忽刽。第二個(gè)參數(shù)便是一個(gè)Factory對象天揖,而這個(gè)Factory對象用于創(chuàng)建ViewModel。然后調(diào)用get方法內(nèi)部構(gòu)造出一個(gè)key跪帝,用于從ViewModelStore中取出ViewModel

而旋轉(zhuǎn)后保存數(shù)據(jù)就通過onRetainNonConfigurationInstances方法構(gòu)建一個(gè)NonConfigurationInstance對象今膊,將此時(shí)的mViewModel對象設(shè)置進(jìn)去,然后存儲在ActivityClientRecord中伞剑。在Activity進(jìn)行relaunch的時(shí)候就會(huì)傳入之前的lastNonConfigurationInstances斑唬,這樣ViewModelStore是上一個(gè)的,自然ViewModel也是上一個(gè)的黎泣,完成了數(shù)據(jù)的保存

當(dāng)Activity正常銷毀時(shí)恕刘,則會(huì)通過clear方法清除所有的ViewModel


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市抒倚,隨后出現(xiàn)的幾起案子褐着,更是在濱河造成了極大的恐慌,老刑警劉巖托呕,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件含蓉,死亡現(xiàn)場離奇詭異频敛,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)馅扣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門斟赚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人差油,你說我怎么就攤上這事拗军。” “怎么了蓄喇?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵发侵,是天一觀的道長。 經(jīng)常有香客問我公罕,道長器紧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任楼眷,我火速辦了婚禮铲汪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘罐柳。我一直安慰自己掌腰,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布张吉。 她就那樣靜靜地躺著齿梁,像睡著了一般。 火紅的嫁衣襯著肌膚如雪肮蛹。 梳的紋絲不亂的頭發(fā)上勺择,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音伦忠,去河邊找鬼省核。 笑死,一個(gè)胖子當(dāng)著我的面吹牛昆码,可吹牛的內(nèi)容都是我干的气忠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赋咽,長吁一口氣:“原來是場噩夢啊……” “哼旧噪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起脓匿,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤淘钟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后陪毡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體日月,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡袱瓮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年缤骨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了爱咬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绊起,死狀恐怖精拟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情虱歪,我是刑警寧澤蜂绎,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站笋鄙,受9級特大地震影響师枣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜萧落,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一践美、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧找岖,春花似錦陨倡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蜜唾,卻和暖如春杂曲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背袁余。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工擎勘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人泌霍。 一個(gè)月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓货抄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親朱转。 傳聞我的和親對象是個(gè)殘疾皇子蟹地,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容