Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProvidePresenter is called earlier than Lifecycle.Event.ON_CREATE #233

Open
victorlapin opened this issue Nov 13, 2018 · 0 comments
Open

Comments

@victorlapin
Copy link

Hi guys, I'm using Moxy + Koin combo in some of my projects, and currently decoupling some lifecycle-aware code. For example, a separate LifecycleObserver for managing Koin scopes looked like a nice idea. However, it doesn't work because presenter (a scoped injection) is called before ON_CREATE event was fired (so no scope created yet). Maybe I'm doing something wrong?

A simple example:

class LifecycleAwareScope(
        lifecycle: Lifecycle,
        private val scopeName: String
) : LifecycleObserver, KoinComponent {
    init {
        lifecycle.addObserver(this)
    }

    private var mScope: Scope? = null

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun createScope() {
        mScope = getKoin().createScope(scopeName)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun closeScope() {
        mScope?.close()
    }
}

fun AppCompatActivity.addScope(scopeName: String) =
        LifecycleAwareScope(this.lifecycle, scopeName)
abstract class BaseActivity : MvpAppCompatActivity() {
    abstract val scopeName: String

    override fun onCreate(savedInstanceState: Bundle?) {
        this.addScope(scopeName)
        super.onCreate(savedInstanceState)
    }
    
    // rest of the code here
}
class MainActivity : BaseActivity(), MainActivityView {
    override val scopeName = Const.ACTIVITY_MAIN

    @InjectPresenter
    lateinit var presenter: MainActivityPresenter

    @ProvidePresenter
    fun providePresenter() = get<MainActivityPresenter>()

    // rest of the code here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant