What's the recommended way to deal with Singletons scavenging in Android (Kotlin)?

I was having some weird problems in my android app when I closed it with a reverse key and re-login and found that they happen because the variables in the 2 Singleton classes I am missing are never overwritten (problem does not occur if manually killing the application from the task manager).

Even after I call finally onBackPressed on the main operation and I see onDestroy being called, the singletons are still in memory.

I decided to do a manual cleanup of these singletons before destroying my activity, the problem is using Kotlin. I would need to make all my member variables null (?) And it would be very difficult to remember to assign null to each variable, so for now I decided to treat the Singleton instance like this and just make the entire instance null on cleanup :

class SingletonName {

    companion object {
        private var _instance: SingletonName ? = null
        private var instance: SingletonName ?
            get() {
                if (_instance == null) {
                    _instance = SingletonName ()
                }
                return _instance
            }
            set(value) {
                _instance = value
            }

        @JvmStatic fun get(): SingletonName {
            return instance!! //avoid having to deal with nullable value in client code
        }
    }

    //need to make sure to call this before destroying main activity
    fun cleanup() {
        instance = null
    }
}

      

Client Usage:

SingletonName.get().somefunc()

      

They seem more complicated than they need to be, and I know it's not even thread safe, but keep in mind that just using the keyword "object" instead of "class" will not work as it will prevent any initialization the second time I I start the application (the constructor is not called again and I cannot get the instance destroyed).

This makes me think it might be worth taking a look at Android Dependency Injection, but I feel like at least one of my singletons really needs to be accessed in many places, so it might not be a neat solution (maybe refactoring this to smaller functions).

The main issue here is how android deals with destruction, but I think I cannot change that, I see no reason why it keeps all static values ​​of inactivity classes if the app's actions are already destroyed.

The question is, how do you handle these cases, regardless of the language used? or what are the best practices in this case in your opinion?

Edit: Actually I just did a test with Dagger 2 and the same problem occurs if I use @Provides and @Singleton, the values ​​of the member variables on the provided object in the second run of the application are not zero, so I guess this is what I need it's up to me explicitly, unless I miss any more comments to inform Dagger that the instance should be released at the end of the app's lifecycle.

+3


source to share


1 answer


creating a Singleton in kotlin is as easy as

Take as an example

No need to explicitly create the class, just do the following

 object MySingleton   {

      fun myFunction() {

        }
    }

      



And call it like

MySingleton.myFunction()

      

you are reading more about singleton in kotlin from their doc https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations

+1


source







All Articles