Keep a class object alive forever - android

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.

Related

Android, retain values in Singleton for Background service

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

Android - Idle condition data loss

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.

Saving Android application data on app exit

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.

Data on a Extended Application class lost when app is closed

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.

How long does my subclass of Android's 'Application' class stick around for?

In my application, I use a subclass of the Application object to store some references to complex objects I need to access from all of my activities. When my app starts, the startup activity checks one of these references, in this case a Location, and if it is null, it starts the LocationListeners which populate the reference for further use.
If I back out of the app to the launcher screen, and re-launch it, the Application object still has the reference from the previous use a few moments prior. This is fine, and is what I'd expect, but I'm curious how long the Application object is kept around once I've back'ed out of my application? (onDestroy() has been called on all activities, nothing in the stack.)
When is it finally killed? I know it does finally get killed as when I've not used the app in a while, it will search for location on startup (indicating the aforementioned null reference.)
(Also, is storing refs there like that a good idea?)
Thanks in advance gang! :-)
It stays around as long as the application is in memory. It only finally goes away when either someone forcible terminates your application (either using a task killer or from the application settings) or when Android decides it wants to reclaim the memory that your app is using (and this typically only happens when your app has been closed for a while and the phone is running low on memory).

Categories

Resources