I have an intentService in my application that is scheduled to be called every 5 minutes. In that service I am using values shared with the rest of the app (Strings, int..). I am using Singleton to hold these values. This works fine most of the times.
However sometimes, for no reason (at least that I can see), the values are lost, and the app crashes due to that. It seems that the Singleton looses it state.
I have read that Android can delete information from memory that it finds unnecessary. For this reason in my activities, I have saved the values in a bundle when the app goes idle (onPause and onResume).
Now the problem is that in the intentService the case is different. I am calling the service every 5 minutes using a timer.
I need a way to retain the values in the singleton. I would rather not save them in the database (it seems a heavy task to do this every 5 minutes), there should be a simpler way.
I hope that was clear, I am grateful for any insight.
If your process is terminated by the system, all the data in memory will be lost. You cannot rely on your process being alive all the time so that you can keep that data in your singleton. Either store that data in a SQLite database, store it in shared preferences, or save it to a file that you can read from the next time your singleton is initialized.
What I doubt, as you use Singleton class in your application, might be android kill your application instance on memory low scenario. So you can lost your singleton class object also. Instead of this, I suggest you have to use SharedPreferences
Related
How can make a class object alive forever after initialise it? because the library needs about 4 minutes to create its object. after that the application will works fine. but, when i close the application, the object get destroyed and again i have to initialise the same object which takes 4 minutes. it is not user friendly(runs in an async task). is there any way to keep the object alive forever (even after application destroyed etc). or please help me to reduce the initialising time to 2 or 3 seconds. please help me brothers.
What you're asking is to find a way to make an object persist in memory after the application releases said memory. Your best bet is to find a way to serialize the object to some non-volatile storage (ie: the phone's storage), and then load from there upon relaunching the application. I cannot specify how to do so because I do not know what type of object this is.
Now I'm facing some problem in Android when memory is low or the application is killed by system.
Scenario 1:
I set some static members in a class, I found in some situation , it will be deleted by system when the application is still running.
My problem to this is : when does this kind of GC run?
Scenario 2:
If I switch to another large application and then switch back to my application ( named App_A). App_A sometimes will be recycled by system and restart the last activity when it be switched back.
But there are some application-wide data (like login info) I saved in a singleton.
My problem to this is : Dose the application-wide data saved in singleton will be deleted?
If so, is there a appropriate way to restore the data?
My effort is:
To Scenario 1, I will avoid to use static member directly.
To Scenario 2, I will save those data into file , after it be deleted, I pass Context to each public function to let each of them have the ability to restore the data. But I think it will be unfriendly when the function is used in some situation which need run quickly.
I can only answer about Scenario 2.
Android will try to keep recently used apps in memory, but if the user switches to another app and memory starts running low, the OS has the option to kill the recently used app to make more memory available to running applications.
I had the same problem, where I had some user-context data like username in a static singleton. This data would disappear when returning to the app after using a number of other apps.
The way I solved this problem was to use the activity's intent. Since the user data was retrieved at the beginning of the app, I would simply pass this data to subsequent activities in their intents. Because the OS stores the intent and uses it to recreate an activity not in memory, my data was no longer vulnerable to being garbage-collected.
Also consider other means of persisting data: Shared Preferences, file system, SQLite database. But never count on static data from previous execution being available at the start of an activity.
It is generally bad idea to use singleton to save some data.
Best practice is using any persistent storage - SQLite, Realm,JSON, or any file.
Easiest way is saving your login data for ex. in JSON - then in Application class parse it in onCreate method into POJO - then you can get it from any place of your app. And store to file when app is closing or on any change.
Anyway I suggest you to read Android guides about persistence, memory management and performance tips.
Is there any way to avoid static data loss in Android if device is kept idle ?
I am having static object which will store some values from activity UI.
If device is kept idle for 4-5 hours that static object will be removed to use memory for other running applications.
If I am doing it wrong, is there any other way to avoid this ?
You can't avoid it. Android can kill your process at any time and does not guarantee to call you back.
This only happens if your app goes to the background. Save your data in onPause() and reload it in onResume(). See the Android documentation for Activity lifecycle for more information.
No, that's how Android is meant to work.
Simply, if data are meant to be persistent, ie if its lifecycle spans beyond the one of the Activity which created it, save it on a persistent storage (ie internal memory), in a flat file or in a database depending if the data has an inner structure or not.
It seems that there is a large amount of information about saving Activity state, but I have been unable to locate much on finding Application state.
I am looking for some design ideas to solve a problem I have run into. I am developing a game that has a fairly large data model (1-2 MBytes). This model exists outside of any Activity, in fact there are numerous activities that all interact with it. These activities are transient, coming and going all the time.
I currently keep a pointer to the data model in my application and all of the activities access the data model through it. I need to save that data model in the event that my application is being killed, but it is far too slow to save it every time an activity hits onPause, which happens very frequently as activities come and go.
What I need is a way to determine that my application (and along with it my data model) are being destroyed. I have searched extensively for this method or callback and have come up empty.
I would appreciate any suggestions.
I have been unable to locate much on finding Application state.
That's because there is no "Application state" in Android, any more than there is in a Web app.
but it is far too slow to save it every time an activity hits onPause
While your entire data model may be "1-2 MBytes", but the amount of data that changes is going to be a small subset of that, for any given change. Use a background thread and only modify the data that has changed.
which happens very frequently as activities come and go
It sounds like perhaps you have too many activities.
What I need is a way to determine that my application (and along with it my data model) are being destroyed
That is not possible. You will never find out that you are being destroyed. Android can and will terminate your process without warning, either at user request (e.g., Force Close, task killer) or for OS reasons (e.g., need the RAM to handle an incoming phone call).
You are welcome to use onUserLeaveHint(), which is called in a number of cases when you entire app loses the foreground, but I certainly would not count on that for something as important as persisting a data model.
I´ve an Android Application that holds some static objects on an class that extends Application class, using the same approach as exemplified here.
The objects that is hold by this class is shared and manipulated between all activities on my app.
Everything works well, but, some time ago, I noticed that when the application runs on backgroud for some time, when it´s restored, the data that was stored on the extended class has gone, and the app starts to throws a lot of NullReference exceptions.
I think that this happens because of the application was being temporary destroyed by the OS, to be recreated when we need to use it again.
So, how could I handle this scenario? Is there any way to discover that the application is being temporary destroyed, without subscribing to the onDestroy event of an Activity? On a test that I did, the onDestroy event was not called when I asked the background process of my app to being stopped.
Thanks a lot!
There is no way to determine when the proccess will be killed, so you always should store important data somewhere(sd-card for example) and restore it in onCreate() method of your App class.
Also take a look at onLowMemory() and onTrimMemory(), release all unnecesary data in memory to help the OS prevent destroying your app, cause one of the reason to determinate your app is a lack of memory.
No, there's no way to tell when you need to persist data that you store in static variables like that. There's no callback that the systems notifies you about this, at least to my knowledge.
So you should only use static variables to store temporary data, or cache data accessed from peristent sources. I've faced this problem in many of my projects, and I always ended up using intents / shared prefs / sqlite / etc. to reliable store data across Activities.