Suppose that my application has many processes.
I try to run some init code (Actually store the application context in a static variable) in Application onCreate(). But I find that in some process, onCreate() is not run before other code in the same process and hence can not access the cached Context.
Where should I put the init code (store the application context) so that it must be ran before any code in the same process?
Suppose that my application has many processes. I try to run some init code
Share
Based on the discussion we had in chat, you have a problem because you are trying to get the application’s Context during static initialization of some helper class. I suggested the following:
If your helper class is only used by Android components
then by the time an Android component calls your class
the Application.onCreate() will have been called
and you can get the context from there.
You just need to wait until you get an actual call from an Android component to go and get the Context.
Don’t try to do it as soon as the helper class is instantiated. Only do it when you need it.