我正在使用Kotlin 的Koin个图书馆进行DI

Koin providing by viewmodel() for get instance of ViewModel by sharedViewModel() to get same instance in fragments.

How can I get same instance of the ViewModel in activities ? I didn't find any way to achieve this.

推荐答案

After some research or discussion on architecture level and also report and issue github Koin,i found solution for this In this scenario,We should save that state/data into Repository which we need to share between multiple activities not in the viewModel and two or more different ViewModels can access same state/data that are saved in single instance of repository

see here

Kotlin相关问答推荐

如何确保Kotlin子类已完成初始化?

使用另一个对象的列表创建对象

如何将 `throw` 放置在辅助函数中但仍然具有空安全性?

从 HashMap 检索时的 NPE,即使 containsKey() 在多线程环境中返回 true

使用 Discord4j 交叉发布 Discord 消息

Kotlin:伴随对象内的函数扩展

奇怪的 cotlin check Not Null 参数错误

Kotlin:如何使用第一个参数的默认值进行函数调用并为第二个参数传递一个值?

如何有效地填充 Gradle Kotlin DSL 中的额外属性?

jetpack compose 将参数传递给 viewModel

无法在 kotlin android 中以编程方式禁用 EditText

将 Android Studio 升级到 3.1.0 后的 Android Support 插件错误

取消信号上的kotlin流量采集

未在IntelliJ IDEA上运行临时文件

在Kotlin中不带类直接引用枚举实例

Jacoco在Gradle 7.0.2和Kotlin 1.5.10上失败

Android EditText 协程go 抖操作符,如 RxJava

在kotlin中初始化类变量的正确位置是什么

保存对象时未填充 Spring Boot JPA@CreatedDate @LastModifiedDate

Java的Kotlin:字段是否可以为空?