Search code examples
androidviewmodel

Why has ViewModelProviders been deprecated?


Why has ViewModelProviders been deprecated? I want to understand the reasons this class has been deprecated. Is it because google used Service Locator design pattern ( so called anti-pattern ) in it ?


Solution

  • If you look at the source code, you'll see that the ViewModelProviders is nothing but a wrapper around a new constructor for ViewModelProvider:

    public static ViewModelProvider of(@NonNull Fragment fragment) {
        return new ViewModelProvider(fragment);
    }
    

    So, as per the deprecation message, just use new ViewModelProvider(this) instead of ViewModelProviders.of(this).