Koin: How to use with custom view?

Created on 24 Feb 2018  路  4Comments  路  Source: InsertKoinIO/koin

I can inject dependencies in activities and fragments, but can I inject in custom view?

class AppInputView : SearchView {
    private val router: Krouter by inject() // Unresolved reference: inject

    init {
        this.onQuerySubmit {
            context.toast(it)

            this.onActionViewCollapsed()
            this.clearFocus()
        }
    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
}

Most helpful comment

You can add the KoinComponent interface to your component, to unlock by inject() properties.

All 4 comments

You can add the KoinComponent interface to your component, to unlock by inject() properties.

Yes. It works. Thank you for great product!

can you please give an example on how to inject Service into a Custom View class ?

if my CustomView class extends FrameLayout, how can i use the KoinComponent interface?

Thanks a head

can you please give an example on how to inject Service into a Custom View class ?

if my CustomView class extends FrameLayout, how can i use the KoinComponent interface?

Thanks a head

Maybe this:

class MyCustomView(context: Context) : FrameLayout(context), KoinComponent {
  ...
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AHarazim picture AHarazim  路  3Comments

Jeevuz picture Jeevuz  路  4Comments

iRYO400 picture iRYO400  路  3Comments

congtuyenvip picture congtuyenvip  路  4Comments

miladsalimiiii picture miladsalimiiii  路  3Comments