0

I am facing an issue with my custom class holding some data. That class is singleton i have many variables there holding my data.

But after some hours maybe 2 or 3 i got empty variables no values.

why is it happening?

if it is due to android default memory management then how to over come this problem ?

I have many background tasks which are depending on the singleton variables when variables are empty then all functionality of my application lost.

OcuS
  • 5,320
  • 3
  • 36
  • 45
farrukh
  • 627
  • 4
  • 16
  • 6
    Because we do not have cristal balls, we need CODE. – OcuS Jun 06 '13 at 07:30
  • 2
    For persisting data without any loses use shared pref or sqlite, the data stored in object and class is for temporary use and will be erased when user kill the app from task manager or may be forcefully closed by OS under certain conditions – Arpit Garg Jun 06 '13 at 07:33
  • i am working on an app which have sync feature sync starts after 5 minutes if application is in idle mode, i send the application in background by pressing home key not the back key. application starts sync and downloading data after 4 hours i resume the application and there is no data in arrays in singleton. – farrukh Jun 06 '13 at 07:44
  • @Arpit Garg can we consider that is due to the OS killing? – farrukh Jun 06 '13 at 07:45

1 Answers1

4

Android may kill your application any time due to any of the reasons given below:-

  1. App was running in background and android needed some memory for the front running Applications.
  2. Some task killer killed the App.
  3. You killed the App from the Manage Applications screen.
  4. Android OS shutdown or restart.

Now to overcome this issue you can use following approach, the best one depends on your requirements:-

  1. If your singelton class has some threads which you need constantly running than you can make this class a Service and with the START_STICKY Attribute your app will become less likely to be killed by Android see tutorial here.
  2. If number of variables are limited you can use SharedPreferences and inside OnCreate()/OnResume of App you can get those values back see similar question.
  3. If you have fairly large number of variables it will be great to store the values in an XML file i.e. <variable name>value</variable name> and Android provides an excellent mechanism to read/write XML files see example here and this detailed tutorial.
  4. You can also store the data in database and read that back when the App starts for that see this tutorial.

These approaches are not mutually exclusive and you can use any combination of them, In my App I used first three. However I would also recommend to check memory usage of your App so that we can know the exact reason for this behaviour. If your app was killed when it has the user focus (running in front) than it is something to worry about. For reference purpose see following similar questions,

Similar Question

Another Similar Question

Application Fundamentals

Community
  • 1
  • 1
Amit
  • 13,134
  • 17
  • 77
  • 148