0

I have a 56 drawable in my res folder.
I use them in 2 Fragments.

Is it a good memory approach to create a class that has a static array containing those drawables and call its get method whenever I want to use it?
Or Should I create a private array in each Fragment?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
George2456
  • 340
  • 5
  • 19
  • 1
    Android automatically caches Drawables obtained from XML. You do not need to create your own cache. – alanv Apr 27 '15 at 18:08
  • Yea I know that but I need to set images of the 57 teams into a gridview once and on a listview as well. The listview fetches data from server which has a number of the logo in the array to set it in the adapater – George2456 Apr 27 '15 at 18:22
  • 1
    Oh, in that case you might as well make a static array. An array of 56 ints is basically nothing in the grand scheme of things. You won't be modifying it, right? – alanv Apr 27 '15 at 18:29
  • Not really it stays the same. The heap reaches 20 mb with 5mb free, is that considered good enough? – George2456 Apr 27 '15 at 18:32
  • 1
    A static 56-int array would take roughly 228 bytes, so that is the least of your concerns. Check out [this documentation](https://developer.android.com/tools/debugging/debugging-memory.html) to learn more about memory profiling. – alanv Apr 27 '15 at 19:24

2 Answers2

0

you should create private arrays in the fragment. Having them all in another class will means they will never get destroyed even when your not using them taking up memory. In addition static arrays will get deleted when the app goes into the background for too long, so you would need extra code to reload the array when is resumed if the array is null

Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
  • So having private array of resources in each class( I have two classes using resources) would be better than creating one static array in a dependable class and just call it whenever I want it? – George2456 Apr 27 '15 at 17:00
  • And if I did your answer wouldn't it be considered ugly coding? as having 57 lines repeated in every class? Sorry if I ask too much :) – George2456 Apr 27 '15 at 17:09
  • you should definitely not have that in every class. You should be asking yourself why you need this many images in an array. I would bet that the way are trying to do something could be done more efficiently. If you are using the same images in both fragments, and 57 of them there is probably something wrong. could you expand on what you are trying to achieve? – Tomer Shemesh Apr 27 '15 at 17:12
  • Its an application which provides results of a football competition and squads. So in the Results fragment I show the logo(resource) of both teams beside the result of the fixture. And then there's Teams fragment which shows the 57 logo of the team and when I click on it I bring stats of the team. So thats why I need an array of resources in two classes – George2456 Apr 27 '15 at 18:08
  • that sounds like you should be using a list or grid of some sort with an adapter. and then you can load the images as needed from drawable, and it will do garbage disposal for you – Tomer Shemesh Apr 27 '15 at 18:13
  • Yea in results class its a listview. And on the Teams class its a gridview. I have custom adapter for each class. So should I create the array of resources inside each adapter and reference the img by number for instance? – George2456 Apr 27 '15 at 18:18
  • well how are you storing all the teams? but yea somthing like that optimally you would have a class Team with a name, etc etc and an int resId with the drawable id in it and in the adapter and load it in the getView. if you want to have a straight up array you could do the array of drawable ints, as shown below – Tomer Shemesh Apr 27 '15 at 18:20
  • Yea that's how I do it basically I fetch data from server which has data of the match and a number that reference the team logo in the array to set it as in image. I don't think there's another solution to that to be fair. So I should just create in both adapters array of images and set it inside straight ahead in the getview instead of storing the image itself and then call it inside getview? – George2456 Apr 27 '15 at 18:26
  • i dont think you should have it twice though. you should but it in an xml array and access that: http://stackoverflow.com/questions/6945678/android-storing-r-drawable-ids-in-xml-array – Tomer Shemesh Apr 27 '15 at 18:29
  • Wow that almosts solves it and saves way too much memory consumed. Thank you I will give it a try and tell you how it goes! :) – George2456 Apr 27 '15 at 18:35
0

You could just create a static int array using the resource ids, here is an example:

public static final int[] DRAWABLE_ARRAY = {
    android.R.drawable.sym_def_app_icon,
    android.R.drawable.title_bar,
    android.R.drawable.ic_menu_compass
};

Then usage of this array could be something like so:

for (int i = 0; i < DRAWABLE_ARRAY.length ; i++) {
    context.getResources().getDrawable(DRAWABLE_ARRAY[i]);
}

This way you aren't creating objects that take up a decent amount of memory since these are only resources ids

petey
  • 16,914
  • 6
  • 65
  • 97
  • I did basically the same in a class which has only the array of resources as an object and kept calling it when I want it. But seems like it consumes memory – George2456 Apr 27 '15 at 17:01