Please tell me how to work with resources, not from the Activity ? Found such a hint on this resource:

It is necessary to transfer a class to the class constructor, for example:

 SomeClass someClass = new SomeClass(MainActivity.this); 

And in your class SomeClass:

 public SomeClass { Context context; SomeClass(Context context) { this.context = context; } protected void someMethod() { ... ваш код String str = context.getResources().getString(R.string.tvBottomText); } } 

Those. not so easy:

 getResources().getString(R.string.tvBottomText); 

but:

 context.getResources().getString(R.string.tvBottomText); 

But honestly I did not understand. I would appreciate the code.

    2 answers 2

    1. Transferring context to another class (non-Android-related classes) is a bad idea that will cause you big problems in the future.
    2. What exactly you have not figured out? It's all pretty simple here - you create a constructor in your SomeClass , which takes as Context context parameter from Android. When creating an instance of this class using the new operator, pass it the context . Context can be obtained in several ways - through Activity.getApplicationContext() will be more convenient. Next, in some method of your class, use context.getResources().getString(R.string...)
    3. Please send the formatted code later.
    • Please tell me what other ways there are, work with resources not from activations. Hardcore code is also a bad idea. - Alexander
    • Alexander, the only way. Just review the solution architecture. - Oleg Skidan
    • Why always call these getApplicationContext() ? There is a lot of context for what is needed and calling this method everywhere is nonsense. It will be more convenient to set the constant in the final Context context = this; and then just context transfer - Flippy
    • I agree with Sergey. You can create a static field in the application class for this purpose. - Oleg Skidan
    • In Application, created the variable Final Context context = this; - Alexander

    The getResource () method is a Context class method. The Activity class is inherited from the Context class, so you can access this method "directly" in activiti; in classes that are not inherited from the Context class, you need to access this method explicitly via an instance of the context class