Search code examples
androidstatic-variables

static variables in application scope are null when app crashes


I have some static variables in a class which extending the Application class .Now, somewhere in app ,if crash takes place then these are becoming null why it is so.

can i say clearly like, I am declaring those two variables in Application sub class and initializing in other java class when crash occurs these becoming null.my app requirement is after login only those two should be initialized.

For any answer/advice Thanks in advance


Solution

  • Static variables are associated with a class and they will live as long as the class is in the memory(which ceases to exist once your application terminates or once the class gets unloaded, read this for further information). Hence, your static variables cease to exist when your application crashes, and again take up their values as null.

    For persistent value storage of these variables, opt for SharedPreferences.

    Read more about SharedPreferences here. And an example to work with.