1

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.

Bulu
  • 1,351
  • 3
  • 16
  • 37
  • If your flow is `Splash > MainActivity`, try removing `nextIntent.addFlags()` and add `finish()` after `startActivity()`. This is a preferred flow. – Darshan Jul 21 '19 at 15:41
  • @DarShan, I already have finish which I updated now.Let me try the removal of the flag. – Bulu Jul 21 '19 at 21:37
  • You don't need `singleTask` launch mode. This causes more problems than it solves. – David Wasser Jul 22 '19 at 13:26

1 Answers1

1

Your process is being killed while the app is in the background. Android creates a new process when the user returns to the app and it instantiates only the Activity on the top of the stack (this would be MainActivity probably), which means that anything set up in SplashActivity will be null.

You will need to either store your data somewhere else (persistently) OR you will need to detect that Android has killed the process and reinitialize your in-memory data when you detect that.

I've answered many questions about how to do this. Check out these:

https://stackoverflow.com/a/29802601/769265

https://stackoverflow.com/a/27276077/769265

https://stackoverflow.com/a/11249090/769265

David Wasser
  • 93,459
  • 16
  • 209
  • 274