F
F
falstaf2011-10-24 20:45:10
Java
falstaf, 2011-10-24 20:45:10

Android, global data

Good afternoon.

The situation is this. We have an android application and a certain class in it that contains some service data necessary for the operation of almost all application components.
This class always exists and always in a single instance, and, initially, was made in the form of a singleton. However, later it turned out that, if you close the application and run something resource-intensive, then the android releases the memory of the singleton without a twinge of conscience, and as a result, after restoring when accessing the singleton, I get a NullPointerException.
After reading stackoverflow and others, I concluded that in the case of an android, it would be more correct to implement the storage of such global data in the form of fields of its successor to the Application class specified in the corresponding <application ... > tag of the application manifest. Which, however, did not give absolutely any profit - still at a random point in time, users get a NullPointerException when restoring the application after a long stay in the background, or after something very resource-intensive and memory-hungry, which means that the android is still the same nails this used memory (which is strange, because I used to think that when pausing and restoring, Activity and other views are recreated, and the Application object lives from the start of the application to its end).

Actually, the question itself. How can I implement the storage of global data, namely, a single instance of a certain class, and, at the same time, how can I guarantee the existence of this object throughout the entire application launch life cycle?

I would be very grateful for any help, tk. I myself have been unsuccessfully struggling with this task for quite a tangible time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
palmut, 2011-10-25
@palmut

It is better to store data either in SharedPreferences or in cache files. Depends on the size and format of the data itself. And provide access to them through the successor of Application. If there is a lot of data, then it is better to store them in the SQLite database and work through a provider. No one ever guarantees the lifetime of an application - the system itself determines when to unload as needed. Theoretically, you can write your own Service that will “hang” in memory all the time, but application users will not appreciate it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question