Giter Site home page Giter Site logo

Comments (4)

ppg408331701 avatar ppg408331701 commented on May 17, 2024

QQ截图20200606104119

还有一个小问题。我在demo的fragment中想调用getActivityViewModel时,出现ide提示要实现一个getValue方法,但是kotlin我是初学者,请问这种情况是怎么回事

/**
 * 在Fragment中得到父类Activity的共享ViewModel
 * 提示,在fragment中调用该方法时,请在该Fragment onCreate以后调用或者请用by lazy方式懒加载初始化调用,不然会提示requireActivity没有导致错误
 */
inline fun <reified VM : BaseViewModel> Fragment.getActivityViewModel(): VM {
    return ViewModelProvider(requireActivity(),
        ViewModelProvider.AndroidViewModelFactory(this.requireActivity().application)
    ).get(VM::class.java)
}

from jetpackmvvm.

hegaojian avatar hegaojian commented on May 17, 2024

不会受到影响的,因为他们两个都是绑定各自的fragment,相当于独立存在,
当然如果你得数据库对象是单例那肯定会有影响
还有就是两个fragment持有的viewmodel两个都是绑定了父Activity的话,就可以达到共享作用域

from jetpackmvvm.

hegaojian avatar hegaojian commented on May 17, 2024

下面的你多了一个括号 删除泛型前面的那个就好了

from jetpackmvvm.

ppg408331701 avatar ppg408331701 commented on May 17, 2024

嗷嗷,谢谢了。

那个括号是android studio的代码补全的时候自己加的,没注意。
感觉kotlin的代码补全没java的智能

from jetpackmvvm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.