I am building an Android application and I am using a static arraylist which I populate during the Splash Activity so that I can use it throughout my whole application. But when I am exiting and re-enter the application, this array remains full with its previous contents. What is happening please?
2 Answers
If it's static it will only be deleted if the class that holds it is completely unloaded from memory. That probably won't happen immediately, but will at some point in the future. You should manually clear it when you know you don't need it anymore. Or store the data somewhere else.
If you were to really kill your process via ADB (not just by exiting your activities), you will notice that the data will be gone.
Without more details it's hard to say for sure but my guess is you may have a reference to your context somewhere so you application is never being closed hence when you rerun it you're seeing the same data there as it's using the same object.
You can probably confirm this by going into the running processes part on the phone or via the adb to see the process still there. The usual reason is having a drawable and not cleared it's callbacks before exiting which keep a reference to the context which will therefore keep a link to your Application. If your static variable is declared in your activity it will mean it remains there.
ArrayList.clear()in eitheronCreate()oronPause()of Activity to remove all previous contents fromArrayListonFinish()after checkingisFinishing(). Otherwise you would get rid of the data on orientation change.