0

This is how I have organized my data:

  • I've created an Application class in my Android project
  • In the Application Class I have an ArrayList Of Objects
  • In the other Activities I call some methods of Application Class that return to me a portion of the aforementioned list

Now the scenario that puzzles me: In a certain Activity If a button is pressed

  1. I check if there is an internet connection
  2. If there isn't one I start an Intent to open Settings for the user to enable mobile networks or WiFi

when It returns though it seem that the application is destroyed because onCreate is called again but at that time the arrayList in my Application class is this time null. So my questions are

  • why is my activity destroyed and not just paused ?
  • If it is destroyed why my app won't start itself from the beginning but it starts only from the Activity that started Activity Action.Settings?
  • If only the latest Activity is Destroyed and not the whole app, how come the data in Application are lost?

this Is how I've declared the ArrayList of objects in my Application class

        private static ArrayList<PointOfInterest> pois;

EDIT: I put a Log.i() on onDestroy() of the Activity and it was never printed. But on When I returned from the Settings, onCreate() was called (without onDestroy having been called), how is that possible?

Libathos
  • 3,340
  • 5
  • 36
  • 61
  • `why is my activity destroyed and not just paused ?` because OS decide to kill the app(lack of resouces, PMS, etc..., don't bother why) ... `If it is destroyed why my app won't ...` becuase system is "remeber" last activity that you use in your app and back to it `If only the latest Activity is Destroyed and not the whole app...` you are right about it ... conclusion: whole app was killed ... – Selvin Sep 25 '14 at 13:14
  • @Selvin why comment and don't just answer it ? You got me covered either way though thanks very much! – Libathos Sep 25 '14 at 13:19

1 Answers1

1

Static fields can be nullified by the os when the application is in background. So they should always be restored.

see this post

Community
  • 1
  • 1
Jfreu
  • 511
  • 5
  • 10
  • not nullified ... simply new instance off application class is created, becuase the old one was killed by OS ... problem is that he is not set those fields onCreate of Application but in some activity ... there should be no problem if there is only one activity ... but if in ActivityA he is setting those values and ActivityB is on top of stack and application is killed, and he back to ActivityB the code from ActivityA is never called .. – Selvin Sep 25 '14 at 13:05
  • @Selvin Ok But I don't think I can override onSavedInstanceState() in Application class – Libathos Sep 25 '14 at 13:12
  • no ... use some persist storage to save data (SharedPreferences/database) – Selvin Sep 25 '14 at 13:13
  • @Jfreu link that you provide is not wrong(in fact it's very good), but please, rephrase your answer since as i said there is no way that static fields could be nullified (if you don't do by yourself) if app goes background, only possibility is that you have new instance of Application class – Selvin Sep 25 '14 at 13:19
  • @Selvin SharedPreferences are used for saving primitives. I don't want to save Primitives but rather Objects, is the only way out using databases? – Libathos Sep 25 '14 at 13:24