Reputation: 1877
I'm creating a main Android project which uses a class library with some utility classes. This class library will be accessed by other projects, so there will be some configuration information that needs to be passed from the project to the class library. This information will be the same for the whole project, so I`d like to pass it only once, instead of passing as a parameter each time I call a function.
How can I pass this global information to the class library?
This question (Android main project with library project - how to pass setting between projects) suggests creating a setter method to pass the information, but that requires an instance of the library active all the time. I thought about creating a static property, but I heard somewhere that in some cases (when the app is swapped to another app and then swapped back), the classes could be recreated and the static values would be lost.
Upvotes: 2
Views: 1183
Reputation: 5282
You could use the manifest file, when the sdk need a api key this is stored in this place in general so, you can use the same idea.
In your manifest:
<meta-data android:name="my_test_metagadata" android:value="testValue" />
In your java file:
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_test_metagadata");
Upvotes: 2