My Android app comprises several activities: M (main or root), A, B, C…
Below is a possible activity navigation graph:

When my root activity M is being initialized, I cache some parameters (like screen dimensions) as static variables in special class MyUtils to use them later in other activities.
The Kaboom happens when I press Home button in activity say C and then launch a dozen applications. When I return back to my application, it appears that everything has been destroyed. C.onCreate method is being called, but cached parameters appears to be reset.
I would like to start from M, not from C after Android has devastated my application after a long pause. How can I achieve this?
I thought of something like this:
// to be put into all my activities but M:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (MyUtils.GetScreenWidth() == -1)
{
// seems like Android killed my app
finish();
return;
}
// Normal initialization.
// Use MyUtils.GetScreenWidth() to align my ui elements.
}
…but I’m not sure that it’s the best way. What would you suggest?
To be honest, I would do the same or something similar to what you’re doing. A possibly better idea is to have a static
MyUtils.initialize()method, perhaps taking in an application context parameter, that is called at eachonCreate()of each Activity that uses MyUtils.Either that, or store the values each in a
SharedPreference.