0

I have a fragmentActivity that has a tabHost. Every Tab has inside a fragment which shows in the screen some ButtonTable. The following code is the ButtonTable:

public class ButtonTable extends Button {
    private Paint paint= new Paint();
    private Bitmap bmp= null;
    private Table table= null;
    int width= 0;
    int height= 0;
    boolean adjustmentForm= false;
    private WeakReference<Bitmap> resizedBitmap;

    public ButtonTable(Context context, Table table, int width, int height) {
        super(context);
        this.table=table;
        this.width= width;
        this.height= height;
        setWidth(width);
        setHeight(height);
        setBitmapImage();
        setBackgroundColor(INVISIBLE);

    }

   [...]

    public void setBitmapImage(){
        int resurce= R.drawable.rectangle6;

     if(table.getTableType().equals("Circle6")){
         resurce=R.drawable.circle6;
         adjustmentForm= true;
        }
        if(table.getTableType().equals("Circle4")){
            resurce= R.drawable.circle4;
            adjustmentForm= true;
        }

        [...]

        bmp = BitmapFactory.decodeResource(getResources(), resurce);
        if(adjustmentForm){

            bmp = Bitmap.createScaledBitmap(bmp, height, height, true);
        }else{

            bmp = Bitmap.createScaledBitmap(bmp,width, height, true);
        }
        resizedBitmap = new WeakReference<Bitmap>(bmp);


    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);

        if(adjustmentForm){
         canvas.drawBitmap(resizedBitmap.get(), ((width-height)/2), 0, null);
         }else{
            canvas.drawBitmap(resizedBitmap.get(), 0, 0, null);
         }

    }

}

If I move through the tabs for a while, after about 10/15 tab changes, the application stops because of a OutOfMemoryError. Why?

Symon_9851
  • 375
  • 1
  • 3
  • 9
  • your app exceeds the memory allocated for the app in heap and you get out of memory exception. you need to scaled down the image.http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Raghunandan Jun 28 '13 at 14:40
  • well, not in this case ... it becuse he is not calling recycle() on bitmaps ... other things is that using WeakReference has no sens here since he is using "not weak reference" too – Selvin Jun 28 '13 at 14:43
  • look here: http://stackoverflow.com/questions/5697760/android-out-of-memory-exception-when-creating-bitmap – Seraphim's Jun 28 '13 at 14:46
  • Combination of the above 2 comments – Rarw Jun 28 '13 at 14:46

1 Answers1

0

I solved my question. I implemented onDestroyView() inside my Fragment. When you change the tabs, the fragment does not call the onDestroy() (does not complete its life cycle), but it calls onDestroyView() and then it restarts with onCreateView(). The Fragment returns to the layout from the back stack. http://developer.android.com/guide/components/fragments.html

@Override
public void onDestroyView() {
    super.onDestroyView();
    layoutHall.removeAllViewsInLayout();

}
Symon_9851
  • 375
  • 1
  • 3
  • 9