4

I need to use getString() from most of the modules in my application.

But for some strange reason, it is tied to Application or Context, so that means I need to pass to each and every class in my application, the Application reference as a parameter.

This clearly violates one of the most basic principles of object oriented design.

Is there a way around this?

Community
  • 1
  • 1
ateiob
  • 9,016
  • 10
  • 44
  • 55
  • "I need to pass to each and every class in my application, the Application object as a parameter." - You won't be passing the 'object' you'll be passing a 'reference' to the object. Whilst this may have some detrimental effects if you don't approach it in the right way, I don't understand how the concept of passing a reference to an object "clearly violates one of the most basic principles of object oriented design". If we don't pass references to objects as parameters to methods of other objects then OOP wouldn't exist. – Squonk Aug 12 '11 at 03:27
  • Oh, and please cite a reference to explain why you think it violates one of the most basic principles of object oriented design (OOD, which isn't actually the same as OOP). – Squonk Aug 12 '11 at 03:30
  • 1
    http://stackoverflow.com/questions/2993817/is-it-a-good-practice-to-create-a-reference-to-application-context-and-use-it-any – ateiob Aug 12 '11 at 03:56
  • 2
    http://stackoverflow.com/search?q=android+get+context+from+anywhere – ateiob Aug 12 '11 at 04:00
  • Neither of the links you posted prove any 'violation' of one the most basic principles of OOD which, as I said, is not the same as OOP nor indeed the same as programming with Java and Android. The point is that passing an Activity Context can be detrimental in the sense that it may (depending on how badly a programmer writes their code) cause memory leaks. Passing the Application Context, on the other hand, doesn't cause the same problem as the Application is alive for as long as any of its components are. – Squonk Aug 12 '11 at 05:04
  • 1
    See http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html – Squonk Aug 12 '11 at 05:05
  • MisterSquonk +1 for the link to this great article. – ateiob Aug 12 '11 at 19:14

3 Answers3

6

The 'strange reason' is that since the string resources are tied to your application, there is no way to access them without some sort of handle to it (the Context). If most of your classes that are not activities need to access string resources, you might want to rethink your design a bit. A simple way to not depend on a Context is to load the strings and pass them to your classes in the constructor.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
4

Yes, there is a workaround - if you happen to (or can) pass a View (any View-derived class) to the constructor, and you assign it to a data member, then you can access the string resources from anywhere in your class:

String str_via_res = yourView.getContext().getString(R.string.str_via_res);

Otherwise, you will have to pass a Context to every class that needs access to these string resources.

uTubeFan
  • 6,664
  • 12
  • 41
  • 65
  • Thank you! Your tip actually works very well for me since all my classes already have some view in them. +1 – ateiob Aug 12 '11 at 19:15
  • FYI: You will still be leaking memory using this method. Views "have a reference to the entire activity and therefore to anything your activity is holding onto". http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html – Austyn Mahoney Aug 10 '12 at 06:45
2

you can extend android.app.Application class to create a static method to pass on the context across all classes in your application.

Refer : PhoneApp.java

Riyaz Mohammed Ibrahim
  • 9,505
  • 5
  • 26
  • 33