In the below example code from my project, android studio warns me that this is a memory leak. Is Android Studio right? Application class is singleton thus i thought that it is good to store it in my class. What is your suggestions?
public class MyApi {
private static MyApi instance ; // Compiler gives the following warning: Do not place Android context classes in static fields (static reference to MyApi which has field context pointing to Context); this is a memory leak
private Context context; // i need this context inside MyApi class.
public static MyApi getInstance() {
return instance;
}
public static void init(Context context) {
instance = new MyApi(context);
}
private MyApi(final Context context) {
this.context = context;
}
}
public class App extends Application{
@Override
public void onCreate() {
MyApi.init(this);
}
}