As ViewModelProviders.of() is deprecated, How should i create object of ViewModel?

前端 未结 10 1077
情深已故
情深已故 2020-12-14 14:05

I have been trying to create an Object of ViewModel in an Activity but ViewModelProviders is deprecated So what\'s the alternative to create the ViewModel\'s object.

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 14:43

    The simple option for the next several months is to stick with stable or beta versions. ViewModelProviders is only deprecated starting with 2.2.0, presently in an alpha03 release.

    For when you do move to 2.2.0 or higher of the lifecycle dependencies, your options depend on your language:

    • If you are using Java, use the ViewModelProvider() constructor, passing in your activity or fragment

    • If you are using Kotlin, there is supposed to be a by viewModels() property delegate, though I am not finding it in the source code...

提交回复
热议问题