Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Android's getString() without violating basic OOD principles?

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?

like image 340
ateiob Avatar asked Dec 16 '22 10:12

ateiob


1 Answers

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.

like image 171
Nikolay Elenkov Avatar answered Dec 28 '22 06:12

Nikolay Elenkov