I\'ve found the R.string
pretty awesome for keeping hardcoded strings out of my code, and I\'d like to keep using it in a utility class that works with models i
You can do this in Kotlin by creating a class that extends Application and then use its context to call the resources anywhere in your code
Your App class will look like this
class App : Application() {
override fun onCreate() {
super.onCreate()
context = this
}
companion object {
var context: Context? = null
private set
}
}
Declare your Application class in AndroidManifest.xml (very important)
<application
android:allowBackup="true"
android:name=".App" //<--Your declaration Here
...>
<activity
android:name=".SplashActivity" android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
</application>
To access e.g. a string file use the following code
App.context?.resources?.getText(R.string.mystring)
I used
getContext().getApplicationContext().getString(R.string.nameOfString);
It works for me.
##Unique Approach
##App.getRes().getString(R.string.some_id)
This will work everywhere in app. (Util class, Dialog, Fragment or any class in your app)
(1) Create or Edit (if already exist) your Application
class.
import android.app.Application;
import android.content.res.Resources;
public class App extends Application {
private static App mInstance;
private static Resources res;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
res = getResources();
}
public static App getInstance() {
return mInstance;
}
public static Resources getRes() {
return res;
}
}
(2) Add name field to your manifest.xml
<application
tag.
<application
android:name=".App"
...
>
...
</application>
Now you are good to go. Use App.getRes().getString(R.string.some_id)
anywhere in app.
This should get you access to applicationContext
from anywhere allowing you to get applicationContext
anywhere that can use it; Toast
, getString()
, sharedPreferences
, etc.
The Singleton:
package com.domain.packagename;
import android.content.Context;
/**
* Created by Versa on 10.09.15.
*/
public class ApplicationContextSingleton {
private static PrefsContextSingleton mInstance;
private Context context;
public static ApplicationContextSingleton getInstance() {
if (mInstance == null) mInstance = getSync();
return mInstance;
}
private static synchronized ApplicationContextSingleton getSync() {
if (mInstance == null) mInstance = new PrefsContextSingleton();
return mInstance;
}
public void initialize(Context context) {
this.context = context;
}
public Context getApplicationContext() {
return context;
}
}
Initialize the Singleton in your Application
subclass:
package com.domain.packagename;
import android.app.Application;
/**
* Created by Versa on 25.08.15.
*/
public class mApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationContextSingleton.getInstance().initialize(this);
}
}
If I´m not wrong, this gives you a hook to applicationContext everywhere, call it with ApplicationContextSingleton.getInstance.getApplicationContext();
You shouldn´t need to clear this at any point, as when application closes, this goes with it anyway.
Remember to update AndroidManifest.xml
to use this Application
subclass:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.packagename"
>
<application
android:allowBackup="true"
android:name=".mApplication" <!-- This is the important line -->
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:icon="@drawable/app_icon"
>
Please let me know if you see anything wrong here, thank you. :)
The best approach from the response of Khemraj:
App class
class App : Application() {
companion object {
lateinit var instance: Application
lateinit var resourses: Resources
}
// MARK: - Lifecycle
override fun onCreate() {
super.onCreate()
instance = this
resourses = resources
}
}
Declaration in the manifest
<application
android:name=".App"
...>
</application>
Constants class
class Localizations {
companion object {
val info = App.resourses.getString(R.string.info)
}
}
Using
textView.text = Localizations.info
You can use:
Resources.getSystem().getString(android.R.string.somecommonstuff)
... everywhere in your application, even in static constants declarations. Unfortunately, it supports the system resources only.
For local resources use this solution. It is not trivial, but it works.