In Hilt, if we mark a class as Singleton, then the instance remains the same as long as the app process is active. It should be the same for Koin too. If a configuration change happens, then a new object should not be created.
Using Koin: The Singleton object is created again when a configuration change happens. Using Hilt: The Singleton object truly remains singleton, even when a configuration change happens.
Koin | Hilt |
---|---|
Koin_Singleton_NewObjects.mov |
Hilt_Singleton.mov |
To reproduce the issue with Singleton behavior in Koin and Hilt, follow these steps:
-
Clone the Repository:
git clone https://github.com/ksharma-xyz/KoinSingletonBug.git -
Testing Koin
git checkout main
-
Testing Hilt
git checkout hilt
-
Open the Project in Android Studio
-
Run the Application:
-
Observe the Logs:
-
Perform a configuration change (e.g., rotate the device / switch theme).
-
Observe the logs to see if a new instance of the Singleton class
RealRepository
is being created.