I take Context application

 Context context = getApplicationContext(); 

From Activiti, I appeal to a third-party class and pass the context there:

 LS ls = new LS(); ls.setContext(context); 

What should I write in the LS class so that when creating it, it would take the Context of the application without the setContext method?

  • one
    pass in class constructor? or what the question is .. classes are not heirs to the context, they cannot “give birth” to themselves from the context, it can only be transferred there - pavlofff
  • And how can I take an application context in a third-party LS class? For example, to access the folder with the application files, without passing it to the LS class. - kaaa

1 answer 1

Alternatively, you can override the Application class:

 public class App extends Application { private static Context sInstance; @Override public void onCreate() { super.onCreate(); sInstance = this; } public static Context getInstance() { return sInstance; } } 

And further, in the LS class constructor, initialize the corresponding field:

 public class LS { private Context mAppContext; public LS() { mAppContext = App.getInstance(); } } 

But in general, IMHO, there is no need to store the ApplicationContext in a separate field. When you need access to it, just get it using App.getInstance() .

As an addition: when sending a link to an activation somewhere, be very careful, because it is fraught with memory leaks.

  • one
    A bad example, the second part is better to remove, so that no one sees, and in the first to make public static volatile, the bases of the instance. You have the opportunity to look in two places. And it is desirable to give a clear name, AppContext, which is also very important - Shwarz Andrei
  • вторую часть лучше убрать - the second is what it is and why? I think about volatile so far, but I don’t agree with public , because OOP is broken. Where is the possibility of a memory look? Concerning the names I agree, but, IMHO, it is quite obvious what the Context is stored in Application . - post_zeew
  • one
    This is advice, judge for yourself. App.getinstance! = App.context, you confuse just by OOP, because getting an instance of a class is different. But the second version of App.context is transparent; you work with the context class. There is one more trick, if you want to make a singltone inside Application, then in fact the class itself is already Sinton, why invent something? If you had a low-level kernel and you kept JNI in this class, what would you create an instance each time? Of course not, this is an obvious mistake, then the question is how the app differs from the Android Core, for this is the Application class. - Shwarz Andrei
  • one
    And in any case, even the guys with Android know this problem, recently they have expanded the ContextCompact class quite well, think with a simple one? Just to make it more convenient, of course, this solves a number of problems for working with resources, but not all, in general ... In any case, the example is not very successful, I wish that you get an experienced partner on CR. - Shwarz Andrei