Hello, I'm using Koin in my Android (Jetpack Comop...
# koin
s
Hello, I'm using Koin in my Android (Jetpack Comopose) app and everything works well except when I try to change the the device theme while the app is running. When I change it from light to dark-mode or vice versa while the app is open, it will instantly crash.
Copy code
FATAL EXCEPTION: main
Process: com.gomersoftware.servicecompanion, PID: 5757
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gomersoftware.servicecompanion/com.gomersoftware.servicecompanion.MainActivity}: org.koin.core.error.KoinAppAlreadyStartedException: A Koin Application has already been started
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5783)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5674)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7924)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: org.koin.core.error.KoinAppAlreadyStartedException: A Koin Application has already been started
at org.koin.core.context.GlobalContext.register(GlobalContext.kt:44)
at org.koin.core.context.GlobalContext.startKoin(GlobalContext.kt:63)
at org.koin.core.context.DefaultContextExtKt.startKoin(DefaultContextExt.kt:40)
at com.gomersoftware.servicecompanion.MainActivity.onCreate(MainActivity.kt:30)
at android.app.Activity.performCreate(Activity.java:8342)
at android.app.Activity.performCreate(Activity.java:8321)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1421)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) 
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5783) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5674) 
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:71) 
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7924) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
1
w
org.koin.core.error.KoinAppAlreadyStartedException: A Koin Application has already been started
You can’t have 2 running koin applications unless you’re doing context isolation
s
Well I'm not trying to run 2 koin instances at once, but I guess when switching themes it will try to create a new instance without closing the old one. Any idea on how I would solve that? Or is there a official solution that I'm not aware of?
w
Sorry, not too sure about this specific case or how you’re creating your Koin application. Probably there are some lifecycle hooks you could use when the theme switches to know that you should shut down your koin application
Wherever you’re creating your koin application is getting called a 2nd time when you switch themes so that seems like a good rabbit hole to go down
j
Are you starting Koin inside your application class.....sounds like maybe you're doing it in the activity
s
@withoutclass Thank you, you led me on the right track. When switching themes it calls
onDestroy()
So I overwrote it like this and everything works now.
Copy code
override fun onDestroy() {
        super.onDestroy()
        KoinPlatform.stopKoin()
    }
👍 1
j
Should be started from Application class
s
@John O'Reilly Thanks, will do!