ViewModelProviders is deprecated in 1.1.0

后端 未结 22 1438
说谎
说谎 2020-12-07 09:14

Looking at the Google docs for ViewModel, they show the below sample code on how to get a ViewModel:

val model = ViewModelProviders         


        
22条回答
  •  借酒劲吻你
    2020-12-07 10:14

    Actually, what does the ViewModelProviders.of() method do under the hood?

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

    It takes Fragment as an argument, creates ViewModelProvider object and passes the fragment directly to ViewModelProvider constructor.

    We can use the same way too.

    E.g. Before:

    OurViewModel mOurViewModel = ViewModelProviders.of(this).get(OurViewModel.class);
    

    After:

    OurViewModel mOurViewModel = new ViewModelProvider(this).get(OurViewModel.class);
    

提交回复
热议问题