سؤال

My android game holds level layout data in an array list. While testing my level loader I noticed that sometimes my levels were failing to load leaving me with just a black screen. At first I thought it might be my textures not re-loading but I proved this wrong by drawing an object separate from the ArrayList and it drew just fine. Next I used a Log tag to check the size of my ArrayList. As soon as I pressed home and then re-opened my application the Log tag started returning a size of zero meaning all my data had been lost. Now I don't really want to load my level data from file every time onPause is called so is there a way to preserve the data held in the ArrayList?

هل كانت مفيدة؟

المحلول

The problem is you're probably getting garbage collected (i.e. you're pause is becoming a destroy because of memory issues). Implement the onSaveIntstanceState and onRestoreInstanceState methods in your Activity to see if that solves the problem.

Num1 made a good point in the comment that I feel is worth point out. Note that bundles should contain small amounts of data. If your ArrayList is large, that's probably why your activity is getting garbage collected in the same place. If that's the case, you've got one of two options. Explicitly store the data on disk your self in the onDestroy method (that way you only do it when you really need to, writing to disk can take a while), or recalculating the stuff when your activity comes back into focus. This is just something good to think about.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top