Static fields in Application class

Łukasz Izmajłowicz
3 min readJan 9, 2016

--

How not to store shared values throughout your app.

I just feel… tired. Seeing people doing the same mistakes over and over again. What I am talking about? As you can deduct from the title, I want to say once for good…

DON’T STORE STATIC OBJECTS INSIDE THE APPLICATION CLASS.

Why is it so wrong?

Let me explain you in just few seconds, but first let’s understand that android is able to kill your application at any point of its lifecycle. It is nothing special and it can happen quite often. Each time your system gets low in memory, framework can decide to kill your app.

The important thing to keep in mind is that framework will try to recreate state of the application from the point of time when it got killed.

To illustrate the problem, let’s first take a look at very simple implementation of App class.

public class App extends Application {    public static String someValue = “unknown”;}

As you can see, we keep one static field holding simple string value. The intention of programer would be to share this value easily between different components throughout the whole app.

Now, changing this global value may look correct, and in fact it is. Here, our starting activity just reassigns value to test. While being on this screen not much can happen indeed. User can rotate the screen, even kill the app and value will be re-assigned correctly.

// MainActivityprotected void onCreate(Bundle savedInstanceState) {    App.someValue = “test”;
vText.setText(App.someValue);
}

Our application flow let us start another activity(DetailsActivity) where App.someValue will be just displayed as well but not re-assigned.

// DetailsActivityprotected void onCreate(Bundle savedInstanceState) {    vText.setText(App.someValue);}

Now things get a complicated. Here is where all the problems (or maybe confusions) start.

Imagine situation, while being on details screen, application gets killed. Next time you try to open it system will restore state and show details screen (It is done for the user to not notice that application was killed).

Sadly, instead of seeing our test value we see default unknown text displayed. That’s unfortunate…

It all because someValue was not persisted. Because App class was recreated, static values were recreated to it’s default values as well. Noting magically, just the way mobile systems work.

There is no single and easy answer how to solve this issue. The best idea is to store values in persistent storage like SharedPreferences. It gets more complicated when trying to store more complex objects using for example SQLite database.

Unless you expect to have fresh values each time your app is re-created, please put close attention to how you deal with shared objects across whole application. What looks correct at the first look can get tricky later.

Storing data in described way can just produce hard to find bugs and make your life miserable. Good practice, which I use personally, is to test how your app behaves with screen orientation changes and application re-creation.

Do not follow blindly examples written on random blogs. I would suggest to use stackoverflow and read comments to check for others opinions. Then, use your brain, and always think twice before adding extra lines of code!

--

--