Accessing Resource Content from a Static Context
In certain scenarios, it's necessary to access string resources from an XML file prior to creating widgets or performing any significant initialization. However, without an active instance of an Activity, the traditional method of calling getResources() on an Activity object becomes unavailable.
Solution
To resolve this issue, the following steps can be taken:
public class App extends Application{ private static Context mContext; @Override public void onCreate() { super.onCreate(); mContext = this; } public static Context getContext(){ return mContext; } }
Usage
Now, you can access the context using App.getContext() and subsequently obtain the resource content through getResources() (or App.getContext().getResources()). This approach allows you to access string resources from a static context, independent of an active Activity object.
The above is the detailed content of How to Access String Resources from a Static Context in Android?. For more information, please follow other related articles on the PHP Chinese website!