Sometimes I've noticed the value of singleton objects are set to NULL when I bring the app from background to foreground and I believe it's something to do with my launch mode settings
<application
android:name="FooApplication"
android:allowBackup="false">
<activity
android:name="SplashActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="MainActivity"
android:launchMode="singleTop" />
</application>
This is flow Splash -> Main. This is how I start the Main Activity from the SplashActivity.
val nextIntent = Intent(context, MainActivity::class.java);
nextIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
finish()
I use Koin to inject some singleton object from the Application Class.
class ProductManager() {
internal var productsMap: MutableMap<String, String> = ConcurrentSkipListMap(String.CASE_INSENSITIVE_ORDER)
//This is being populated by some method call from SplashActivity
}
Now 90% of the time I can able to see the value exists when I bring the app from background to foreground while I am on MainActivity. However, sometimes ProductManager.productsMap is NULL though ProductManager itself is not NULL.
I assume the MainActivity is killed by the process and data being lost. If data is lost why ProductManager object being injected to MainActivity is not NULL but the property of it is NULL. This shouldn't have happened as Activity is killed but the process still exists as When I launch it still goes to the MainActivity being the current top not relaunching the app again.
I don't want to store the data from Bundle or SharedPreference. Can I able to relaunch the app which would bootstrap the data automatically instead of getting NULL value from the Singleton.