My application has some dynamic paths that are formed at each launch. And to which I need to have access from any part of the program. Now I use a static object. Everything suits me. However, the article on Habré is not recommended to store data in static , it is associated with the life cycle of the Activity.

There is either a property file or SQLite , but I am confused by the "weight" of these methods. Perhaps there are some other options? Or is it worth staying at SQLite ?

  • Read about serialization if you need to store large structured data - BORSHEVIK
  • @Krom Stern, why not answer? 1 - SharedPreferences , 2 - БД , 3 - @Bean . In my case, I will give preference to all the БД . But @Bean is an interesting option, at least not familiar to me. - eugeniuskh
  • My comment was addressed to BORSHEVIK before his answer was transferred to the comments. - Kromster

3 answers 3

It all depends on the size and complexity of your data. If you just store strings / arrays of strings / numbers, you can use SharedPreferences :

 //сохраняем строку в файл внутренней директории приложения SharePreferences pref=PreferenceManager.getDefaultSharedPreferences(context); pref.edit().putString("key", "value").commit(); //получаем ранее сохранённые данные String savedData=pref.getString("key"); 

If something is more complicated, then yes - use the БД .

    For this, they came up with the Singleton pattern.

     @EBean(scope = Scope.Singleton) public class MySingltoneBean { //Тут прописываем геттеры сеттеры и прочие методы доступа к общим переменным } 

    Now, when we need to use our shared data from the Activity, it’s enough to define in it

     @Bean MySingltoneBean mySingltoneBean; 

      I use a slightly different way (the easiest one): I create a Java class (not an Activity , but just a class) and I keep static variables in it. So they are accessible to everyone and from everywhere, as well as do not depend on the life cycle of the Activity . The main thing in this way is simplicity.


      Example:

       class Resources { public static int myNumber = 2334595; } 

      Now the myNumber variable can receive and change any activity (and fragments, and all others).


      Another good way described above. But that way is to store data that should be saved after the application is closed, and mine is when the application is running (you said that they are created at startup).

      • one
        Your method has its drawback, namely GC can still delete the necessary data under certain circumstances - BORSHEVIK
      • @BORSHEVIK, and ... what is GC? - user189127
      • @ bukashka101, probably the garbage collection - eugeniuskh