Reputation: 3865
Per the Android Documentation it states (http://developer.android.com/reference/android/app/Application.html) :
There is normally no need to subclass Application.
My question is that. My app needs to prepare some data (e.g. load some states data from shared preference) at app startup time before it can do anything else. I usually put the data preparation logic in a class that subclasses Application.
If subclassing application is not encouraged, where should I put the app-startup-time data preparation logic? What are the best practices?
Thanks.
Upvotes: 4
Views: 363
Reputation: 1031
Not to drudge awake a super old topic but you can utilize a ContentProvider to initialize items from SharedPreferences without having to subclass Application. (same technique that Firebase uses for their SDK). They are initialized immediately after the Application object is created.
An example can be found here : https://github.com/florent37/ApplicationProvider
Upvotes: 0
Reputation: 6771
I agree with @r2DoesInc and his answer, that it's actually fairly common to do, that doc is probably somewhat outdated.
However what's interesting is that in Google's latest app for I/O 2014, they don't do it.
https://github.com/google/iosched
(the main chunk of code is in) https://github.com/google/iosched/tree/master/android/src/main/java/com/google/samples/apps/iosched
Location proving Application isn't subclassed https://github.com/google/iosched/blob/master/android/src/main/AndroidManifest.xml
Upvotes: 1
Reputation: 3771
Its not that it is discouraged, its just that as they said, it is "normally" not needed.
In your case, you use it correctly, I have done the same in many of my own apps.
Upvotes: 6