My activity have several static bitmap arrays,but sometimes those static bitmap is gone when i open other activity, such as load photo using the intent below:
My Code
Intent intent = newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, ACTIVITY_SELECT_IMAGE);
When i stay too long in intent which i call to pick image, all my static bitmap array is gone.. i can’t use final static because i change those bitmap on run time..
How do i prevent this? Thanks.
Android offers no guarantees on static references like that. The reason that it sometimes works and sometimes not is basically because sometimes the garbage collector(GC) has set your array to null and sometimes not. The GC will not set your reference to null if you activity is active, but it will if it has to when your activity is inactive, e.g. not visible.
You need to add a check for null in the onResume() method and make a new Bitmap array if necessary. As for the bitmaps themselves that are stored in the array, you need to store them in memory. Check the following article and read about saving cache files via the getCacheDir() method. That is basically what you need since the data does not need to be stored persistently across multiple sessions.
http://developer.android.com/guide/topics/data/data-storage.html