Memory-leak free Singleton with context

元气小坏坏 提交于 2019-12-06 04:50:10

Option #1: Have getInstance(Context) call applicationContext on the supplied Context and hold that. The Application singleton is created when your process is and lives for the life of the process. It is pre-leaked; you cannot leak it further.

Option #2: Get rid of getInstance() and set up some form of dependency injection (Dagger 2, Koin, etc.). There are recipes for these DI frameworks to have them supply the Application singleton to the singletons that they create and inject downstream.

When you call the getInstance() for the first time, the Context that you pass to this function is saved forever. So, the context in further getInstance() calls doesn't have anything to do there. I never save this Context.

This is what I am doing:

Create an object in Kotlin and initialize the object with a context as soon as the app starts. Instead of storing the context, I perform whichever operation is required with that context.

object PreferenceHelper {

    private var prefs: SharedPreferences? = null

    fun initWith(context: Context){
        if(prefs == null) this.prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
    }

    fun someAction(){ .... }
}

and inside the Application class:

class MyApp: Application(){
   override fun onCreate(){
      PreferenceHelper.initWith(this)
   }
 }

and later anywhere in the app:

 PreferenceHelper.someAction()

You can do this if you don't need a reference to the Context every time you perform something with the Singleton class.

I would not store the context in the SingletonClass, I would simply pass the context to each method of the class via dependency injection. Something like:

SingletonClass.callMethod(context)

Define the "static" method in the companion object like that:

 companion object {
        fun callMethod(context: Context) {
            // do Something
        }
    }

Then call it from your activity with:

SingletonClass.callMethod(this)

Hope that it helps :)

if You must create singleton class with context involved, you can do it like this. This will help. In this case, your context will be reset in every activity, when you call getInstance(context).

public class MyClass {

    private Context context;

    public static getInstance(Context context){
         if(instance ==null)
             instance = new MyClass();
         instance.setContext(context);
         return instance;
    }

   public void setContext(Context context){
      this.context = context;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!