我是靠谱客的博主 昏睡黑裤,最近开发中收集的这篇文章主要介绍java modeland view_java-架构组件:ViewModelProvider如何知道要调...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在代码段中,有一个条件可以检查modelClass是否为AndroidViewModel类型(继承自ViewModel),该构造函数采用Application参数.这更像是排他性的案例,它使Factory无需查找与特定参数匹配的构造函数.

该提供程序在创建时会查找与提供程序参数匹配的构造函数:

public class ViewModelParameterizedProvider {

private AtomicBoolean set = new AtomicBoolean(false);

private ViewModelStore viewModelStore = null;

static ViewModelParameterizedProvider getProvider() {

return new ViewModelParameterizedProvider();

}

@MainThread

public static ViewModelProvider ofSupportFragment(Fragment fragment, Object... params) {

return getProvider().of(fragment).with(params);

}

@MainThread

public static ViewModelProvider ofActivity(FragmentActivity fragmentActivity, Object... params) {

return getProvider().of(fragmentActivity).with(params);

}

@MainThread

public static ViewModelProvider ofFragment(android.app.Fragment fragment, Object... params) {

return getProvider().of(fragment).with(params);

}

private ViewModelParameterizedProvider of(Fragment fragment) {

checkForPreviousTargetsAndSet();

viewModelStore = ViewModelStores.of(fragment);

return this;

}

private ViewModelParameterizedProvider of(android.app.Fragment fragment) {

FragmentActivity fragAct = (FragmentActivity) fragment.getActivity();

return of(fragAct);

}

private ViewModelParameterizedProvider of(FragmentActivity activity) {

checkForPreviousTargetsAndSet();

viewModelStore = ViewModelStores.of(activity);

return this;

}

private ViewModelProvider with(Object... constructorParams) {

return new ViewModelProvider(viewModelStore, parametrizedFactory(constructorParams));

}

private void checkForPreviousTargetsAndSet() {

if (set.get()) {

throw new IllegalArgumentException("ViewModelStore already has been set. Create new instance.");

}

set.set(true);

}

private ViewModelProvider.Factory parametrizedFactory(Object... constructorParams) {

return new ParametrizedFactory(constructorParams);

}

private final class ParametrizedFactory implements ViewModelProvider.Factory {

private final Object[] mConstructorParams;

ParametrizedFactory(Object... constructorParams) {

mConstructorParams = constructorParams;

}

@Override

public T create(Class modelClass) {

if (modelClass == null) {

throw new IllegalArgumentException("Target ViewModel class can not be null")

}

Log.w("ParametrizedFactory", "Don't use callbacks or Context parameters in order to avoid leaks!!")

try {

if (mConstructorParams == null || mConstructorParams.length == 0) {

return modelClass.newInstance();

} else {

Class>[] classes = new Class>[mConstructorParams.length];

for (int i = 0; i < mConstructorParams.length; i++) {

classes[i] = mConstructorParams[i].getClass();

}

return modelClass.getConstructor(classes).newInstance(mConstructorParams);

}

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

return null;

}

}

}

最后

以上就是昏睡黑裤为你收集整理的java modeland view_java-架构组件:ViewModelProvider如何知道要调...的全部内容,希望文章能够帮你解决java modeland view_java-架构组件:ViewModelProvider如何知道要调...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部