There is an article on the Android ’s official website, where it is recommended to save volumetric objects (or rather their links) from the Activity , at the time of re-creation, in a fragment (which does not have a UI and is not re-created).

But I still can not understand: why can not use singletons for the same purpose? You can get it from any instance of the Activity (it doesn't seem to lose links either :), so what are the problems and why is this not the easiest way to save?

  • a singleton will hold links and provoke memory leaks (do not give activations that are already closed to destroy them to the garbage collector), at a minimum. In general, singleton is declared anti-petter in the PLO, you have to think about how to solve problems without it. - pavlofff
  • @pavlofff and the fragment will not keep links? - rjhdby
  • The @rjhdby fragment is part of the activation, not the external static object and will be destroyed along with it. In general, all the problems due to re-creation when turning and there are several ways to solve this, retain-fragment is convenient in a number of uses. but not the only solution. - pavlofff
  • @pavlofff and if we do not need to destroy the data when you destroy a specific activation? - rjhdby
  • @rjhdby if not needed, then do not destroy. Save them to external storage (file, database, preferences). No need to interfere with a bunch of flies and cutlets, here we are talking about recovery after re-creation, and not saving - Singleton is in any case not a good solution for long-term data storage. - pavlofff

2 answers 2

It is possible in Singleton , with one amendment: you must use the natural Android singleton Application class, which can be obtained anywhere in getApplication() . Technically, you need to create your own class:

 public class MyApplication extends Application { private MyData myData; private static MyApplication me; public MyApplication() { me=this; } public static MyApplication getApplication() { return me; } public MyData getMyData() { return myData; } } 

Now in any place you can call:

 MyApplication.getApplication().getMyData(); 

or through context (if you don't like it)

 ((MyApplication )context.getApplication()).getMyData(); 

PS One should not forget to declare MyApplication in the manifest.

  • Just now, following the link shown by @pavloff, I read that, after closing the application due to lack of memory, only the upper Activity and Application will be restored, respectively, the usual Singleton will already be different. Do you therefore recommend using Application as a singleton or are there other reasons? And you can tell where you can get information on the topic of restoring the application if it is closed, about the behavior of objects in Android , etc.? - Rostislav Dugin
  • @RostislavDugin on issues of interest to you can be read in almost any high-quality literature on the development under Android (books). Articles on the Internet as a rule do not give complete information, except perhaps developer.android.com - here is collected EVERYTHING that an android developer needs to know. - pavlofff
  • @pavlofff, that is, you can safely take and systematically study the entire site? - Rostislav Dugin
  • @RostislavDugin If English is good, then yes. if it is bad, then first it is better to read in Russian, for example, a book on android development by Brown Hardy, second edition. In the corresponding section on the forum w3bsit3-dns.com there is a lot of good literature on the last pages 20-30 somewhere. - pavlofff
  • @pavlofff, with English, so far, not so good, but it’s pretty easy to write to android.developer (well, or I haven’t seen such articles). - Rostislav Dugin

You can save in Singleton . They mean to store data that is needed only for a specific activation. If you use Singleton , then you have to make sure that this data is deleted after the destruction of this activation. And if you use the retain fragment, then this will not be necessary.

  • Now you have written something completely wrong. Why do I need Singleton to store data in a particular Activity if I can store it directly in this Activity ? And, if you are about capturing Activity Fragment 's om, it can do absolutely the same thing. - Rostislav Dugin
  • Then it is not clear what question you are asking. I explained to you in which case Google recommends storing the data in a fragment. If the data is stored in activation, then it will be destroyed during the coup (if the data is not serializable and not parseable). for such purposes and stored in a fragment or in a singleton. The rest is already described above. In general, you either incorrectly asked a question or did not quite understand what I wrote to you - pavel163
  • @ pavel163 In general, onSaveInstanceState() `onRestoreInstanceState ()` and packing in the Bundle in the onSaveInstanceState() are used to save the state, but if you cannot pack into a bundle, then the retain fragment is already there. - pavlofff
  • @pavlofff Well, I wrote about it in the comments. Did not write it in the answer, since it was not a question. The question was specific about the retain fragment and singleton))) - pavel163