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

KoinAppAlreadyStartedException after changing theme #1840

Open
Cuyer opened this issue Apr 3, 2024 · 5 comments
Open

KoinAppAlreadyStartedException after changing theme #1840

Cuyer opened this issue Apr 3, 2024 · 5 comments

Comments

@Cuyer
Copy link

Cuyer commented Apr 3, 2024

Describe the bug
When changing from light to dark theme or vice versa, following error is thrown:

KoinAppAlreadyStartedException: A Koin Application has already been started

To Reproduce
Steps to reproduce the behavior:

  1. Change theme to from light mode to dark mode or vice versa
  2. App crashes

Expected behavior
App doesn't crash

Koin module and version:
I suppose its koin-compose:3.6.0-wasm-alpha2

I am working on a project that uses compose multiplatform. I've tried using documentation to start koin, but its lacking information regarding compose multiplatform.

Snippet or Sample project to help reproduce

class MainActivity : FragmentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        this.enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        startKoin {
            androidContext(this@MainActivity)
            modules(
                appModule(),
                androidModule()
            )
            logger(PrintLogger())
        }
        setContent {
            AppTheme {
                App()
            }
        }

    }
}
@gamer3dx
Copy link

gamer3dx commented Apr 5, 2024

You initializing Koin inside onCreate in Activity, which is recreated on theme change.
Try to initialize it inside onCreate in Application.

@Cuyer
Copy link
Author

Cuyer commented Apr 5, 2024

I've tried it as such:

class MainApp : Application() {

    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidContext(this@MainApp)
            modules(
                appModule(),
                androidModule()
            )
            logger(PrintLogger())
        }
    }
}

But I get error java.lang.IllegalStateException: KoinApplication has not been started

@gamer3dx
Copy link

gamer3dx commented Apr 5, 2024

Interesting. What are you doing to get this exception? I remember that I encountered this error on first inject call in my pet project when I forgot to register my application class in Manifest file.

@Cuyer
Copy link
Author

Cuyer commented Apr 5, 2024

Yes, that's exactly the issue. I had to register application class in manifest file and everything works now. And I've checked and after chaning theme the app doesn't crash. Thank You!

@kopoh
Copy link

kopoh commented Apr 5, 2024

Omg I have the same problem, I do initKoin() in Common Main:

@Composable
internal fun App() {
    AppTheme {
        initKoin()
        Napier.base(DebugAntilog())
    
        Navigator(screen = FirstScreen)
    }
}
fun initKoin() = startKoin {
    modules(
        sharedViewModelModule,
        LoginViewModelModule,
        SettingsValueModule,
    )
}

and my application crashes already when switching themes using the theme switch button in the status bar.

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

3 participants