Can I rely on statement the Application’s Context is not changing during application’s lifecycle? What if I store a context somewhere using singleton pattern and then use wherever I need?
Can I rely on statement the Application’s Context is not changing during application’s lifecycle?
Share
To answer your second question first: If you need to store some state in a singleton, then you can use the Android
Applicationclass and yourApplicationbecomes your singleton (it is also aContext). There would be no need to store it.To the first question, as to whether the
Contextcan be changed at runtime: sort of related to your other question, I think we can figure that out by looking atContextWrapper.attachBaseContext:So, yes, you can rely on it; it can’t be changed, and if you try, it will throw an
IllegalStateException.