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);
|
}
|
}
|
|
}
|