当前位置:网站首页>Koin simple to use
Koin simple to use
2022-07-27 20:31:00 【Li er'er】
Koin What is it? ? Why use Koin?
Koin Is an injection framework without reflection / agent It is convenient to use objects to call a class object and use the methods inside the class object .
I don't understand , Here is just to teach you how to practice Koin, Only because it is used in the project as a last resort
Koin From an open source Author
Inside Koin 3.2 - Optimizing your Modules with Includes
Its wood is for Kotlin Convenient and fast injection , Before that, there is another open source annotation Degger2 No description here , Self Baidu in need
There are several steps to use :
First 1. What is the object you want to inject , That is, the class you are going to use
Here we use
LoginPresenter For example
class LoginPresenter(val login: LoginModel) {
fun login():Boolen{
return login.do()
}
}2.LoginPresenter Required parameter classes LoginModel
interface LoginModel{
fun do(): Boolean
}
class LoginModelImpl : LoginModel{
override fun do(): Boolean {
// to do login ... Some logic to log in is omitted here
return true
}
}3. Create a new class AppMoudle The classes to be injected Use Koin Of model To register
package com.nova.di
import com.nova.koin.HelloRepository
import com.nova.koin.HelloRepositoryImpl
import com.nova.koin.MySimplePresenter
import org.koin.dsl.module
/**
* @author Created by Li
* 2022/7/14
*/
val appModule = module {
// single Represents a singleton
single<LoginModel> { LoginModelImpl() }
// factory Indicates factory get() Indicates that the parameters registered above are automatically brought in
factory { LoginPresenter(get()) }
}4. stay Application Start injecting objects at
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
// Level.NONE
androidLogger()
androidContext([email protected])
modules(appModule)
}
}
}5. Get the injected object on the called page LoginPresenter
class KoinActivity : AppCompatActivity() {
private val TAG = "KoinActivity"
val loginPresenter: LoginPresenter by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_loin)
if (loginPresenter!= null)
LogUtil.d(TAG, "${loginPresenter.login()}")
}
}
In fact, an error will be reported when it is run after completion , Here is a pit to use when starting injection
androidLogger() Will report a mistake
java.lang.NoSuchMethodError: No virtual method elapsedNow()D in class Lkotlin/time/TimeMark; or its super classes (declaration of 'kotlin.time.TimeMark' appears in /data/app/~~ArLPm6ZwWHpL7fiTI_vDjQ==/com.nova.fraworkproject-QE0TR4fU0SiejzRkgwjc2w==/base.apk)
The following settings should be used *********
androidLogger( Level.NONE)class MyApplication : Application() {
private var mActivitRouter: ActivitRouter? = null
override fun onCreate() {
super.onCreate()
startKoin {
// Level.NONE
androidLogger( Level.NONE)
androidContext([email protected])
modules(appModule)
}
}
}As for how to use it, that's all , If it helps you, please pay more attention ,day day up.
边栏推荐
- Express: search product API by keyword
- Built in functions other functions
- 2019年全球半导体营收同比下滑12%,中国市场份额第一
- 内置函数其它函数
- js跳转页面并刷新(本页面跳转)
- Standing on the shoulders of giants to learn, jd.com's popular architect growth manual was launched
- es6删除对象的属性_ES6删除对象中的某个元素「建议收藏」
- To share the denoising methods and skills of redshift renderer, you must have a look
- 京东:按关键字搜索商品 API
- Pyqt5 rapid development and practice 4.5 button controls and 4.6 qcombobox (drop-down list box)
猜你喜欢

使用cpolar建立一个商业网站(5)

Solve the problem of displaying the scroll bar when there is no data in the viewui table

'vite' is not an internal or external command, nor is it a runnable program or batch file

Wu Hequan: digital technology empowering "double carbon" practice according to local conditions

Redis queue、rdb学习

Unity2d dynamic cartoon script (animation demonstration II for the chapter of Tiger Bridge)

Office automation solution - docuware cloud is a complete solution to migrate applications and processes to the cloud

Redis thing learning

PyQt5快速开发与实战 4.7 QSpinBox(计数器) and 4.8 QSlider(滑动条)

办公自动化解决方案——DocuWare Cloud 将应用程序和流程迁移到云端的完整的解决方案
随机推荐
Add joint control to gltf model
Huawei connect conference 2022 opens Bangkok trip; Facebook pushes the video revenue sharing function, and the creator can get 20% share
If you want to switch to software testing, you should pass these three tests first, including a 3000 word super full test learning guide
Redis 事物学习
MediaTek releases Helio g80, a mid-range game phone chip
DP (dynamic programming)
ES6--拓展运算符运用
es6删除对象的属性_ES6删除对象中的某个元素「建议收藏」
Product Manager: check where there is an error prompt of "system exception" on the offline
shell
图解LeetCode——剑指 Offer II 115. 重建序列(难度:中等)
PyQt5快速开发与实战 4.5 按钮类控件 and 4.6 QComboBox(下拉列表框)
Codeforces Round #810 (Div. 2)B.party(思维题)超详细题解
I'm also drunk. Eureka delayed registration and this pit
Learn about the 12 necessary animation plug-ins of blender
分享Redshift渲染器的去噪方法技巧,一定要看看
IE11 下载doc pdf等文件的方法
Adjust the array so that odd numbers all precede even numbers
uva1421
Why do we need third-party payment?