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

Calling Koject.start() multiple times creates dependencies again and again #286

Open
srikrishnacj opened this issue Aug 18, 2023 · 2 comments

Comments

@srikrishnacj
Copy link

srikrishnacj commented Aug 18, 2023

Currently we call Koject.start() from MainActivity.onCreate. Each time dark theme is toggled, MainActivity.onCreate being called again and so Kojact.start(). This leads to singleton dependencies created multiple times. currently my workaround is

@SuppressLint("StaticFieldLeak")
object ApplicationContextModule {
    lateinit var context: Context
    var start = false

    @Provides
    fun provideContext(): Context {
        return context
    }
}
ApplicationContextModule.context = this.applicationContext
if (!ApplicationContextModule.start) {
    Koject.start()
    ApplicationContextModule.start = true
}

But it would be nice if framework itself detect these changes

@mori-atsushi
Copy link
Owner

In android, I recommend calling Koject.start() in your application class.

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        Koject.start {
            application(this@MyApplication)
        }
    }
}

Please refer to the following documents for details.
https://mori-atsushi.github.io/koject/docs/android/application

@mori-atsushi
Copy link
Owner

Koject.start resets singleton for testing. This is the expected behavior.

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

2 participants