0

i am working on an android app. In the app you can create different movie projects and each project will have clips (stored on external storage that follows a particular directory structure) that you can work with. The project can be in different states i.e. started, on-going, complete. I am using a combination of directory name and shared preferences to save the state of the project. When you open the app, splash screen shows up and in the background I am loading all the projects and movies data in static ArrayList that lies in my main activity class. I only clear the ArrayList when you confirm app exit (done using alert dialog upon backpress). So I only do write operations on list when opening and closing the app.

So far it works fine, but I am still bit skeptical with storing data in static variables. I am not able to foresee any bugs but would like to hear out thoughts from the community on my method.

Karani.pranay
  • 191
  • 2
  • 11
  • 2
    A better, more scalable approach, would be to consider using something SQLite for persistence of certain types of data. The problem with storing everything in the one activity comes when you add a second activity, and the first one goes out of scope, killing off all your static state. – Tim Biegeleisen May 10 '18 at 11:34
  • i think since the variables are static, they are not tied to a specific activity but to the app process. – Karani.pranay May 10 '18 at 13:24
  • According to [this SO question](https://stackoverflow.com/questions/11134686/static-variables-what-is-their-life-span), you might get away with doing this, but there are still several ways your activity could get unloaded. It's probably best not to rely on static state here. – Tim Biegeleisen May 10 '18 at 13:42

1 Answers1

0

A common approach is to use the singleton pattern in which you create an instance of "engine" class which stores and manages application state and data. Activities can access it by calling some static method for example Engine.getInstance() when they need to fetch data. This also helps you to separate logic code from pure UI.

Teemu Lätti
  • 121
  • 1
  • 5