Runt
2022-03-01 cc5e1cbab3de2c497ffb12d4fc49dd331ba20a5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.auto.lyric.base.model;
 
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
 
/**
 * Created by Administrator on 2021/11/4 0004.
 */
public class ViewModelFactory implements ViewModelProvider.Factory {
 
    static ViewModelFactory sInstance;
 
    public static ViewModelFactory getInstance() {
        if (sInstance == null) {
            sInstance = new ViewModelFactory();
        }
        return sInstance;
    }
 
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        //noinspection TryWithIdenticalCatches
        try {
            return modelClass.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("(创建实例失败,没有对应的构造方法,请重写一个实现类)Cannot create an instance of " + modelClass, e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Cannot create an instance of " + modelClass, e);
        }
    }
 
}