Search code examples
androidsingletonandroid-lifecycledagger

Dagger injected singleton is reverting to default value when OS initiates process death


I just want to understand if this (title) is normal behavior or if I have implemented dagger injection incorrectly? The steps I am taking to reproduce this bug is

  1. background app
  2. terminate process through Android Studio's logcat -> red square button (terminate application)
  3. foreground app -> value in the singleton is reverted to default values

background information: I have defined the dagger injected singleton in a module that is part of a larger application.


Solution

  • Your app runs in a single process, once terminated everything that is in memory is removed, including the singleton instance.

    Once you load your app back, Android tries to load the previous screen before the process death and initialize what is needed, thus the new instance of your singleton.

    If you wish to retain data after process death, there are several different ways, but not a singleton.