0
我是kotlin和kodein开发的新成员。 我想将数据注入到一个不扩展的简单类中。kodein,向简单类注入数据
我有我的MainActivity
延伸KodeinAppCompatActivity()
, 我的片段延伸KodeinSupportFragment()
呼吁从我的简单的类CallType
的功能。但是这个函数必须从其他简单类ConnectivitySate
改变布尔值。我不想使用静态值。
下面,我的代码:
class App : Application(), KodeinAware {
override val kodein by Kodein.lazy {
import(autoAndroidModule([email protected]))
bind<CallType>() with instance(CallType())
bind<ConnectivityState>() with instance(ConnectivityState())
bind<ContactData>() with instance(ContactData())
}
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(androidActivityScope.lifecycleManager)
}
MainActivity:
class MainActivity : KodeinAppCompatActivity() {
我的片段:
class JournalFragment : KodeinSupportFragment(){
private val callType: CallType by instance()
@SuppressLint("MissingSuperCall")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initializeInjector()
}
override fun onCreateView(inflater: LayoutInflater?, container:
ViewGroup?,savedInstanceState: Bundle?): View? {
// !! CALL MY FUNCTION !!
callType.call(callType.callNumber)
}
....
@SuppressLint("MissingSuperCall")
override fun onDestroy() {
super.onDestroy()
destroyInjector()
}
我的简单的类:
class CallType {
fun call(number: String) {
// !! I want to change gsmState value from ConnectivityState class
connectivityState.gsmState = true
}
我ConnectivityState类:
class ConnectivityState {
var gsmState = false
}
这是在许多其他的例子,因为在很多情况,我阻止这样的。我尝试很多东西,但我一直有这样的错误:value not injected
非常感谢您的回复..
Thak你,但它的怪异,因为它工作时,我称之为'callType.call(callType.callNumber)',但我会改变它。我的问题是在CallType类中。我想在这堂课中注入价值。但我不知道如何,因为它没有延伸。 – Halima
我真的被封锁了......如果你有任何想法。谢谢你的帮助 – Halima
你可以在构造函数处传递'kodein'属性,然后使用标准的检索方法:https://salomonbrys.github.io/Kodein/#_dependency_retrieval –