0

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?

3
  • use sqlite & insert arraylist in it Commented Jan 3, 2013 at 17:50
  • 1
    use ArrayList.clear() in either onCreate() or onPause() of Activity to remove all previous contents from ArrayList Commented Jan 3, 2013 at 17:52
  • It would make more sense to do that in onFinish() after checking isFinishing(). Otherwise you would get rid of the data on orientation change. Commented Jan 3, 2013 at 17:54

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

if OP clear application data from Application Manager then also data will be gone
Oh, "Stop" it you mean? Yes, that's also true.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.