Search code examples
androidbuttonbitmapimageviewrecycle

Android recycle bitmap when going back


My app lets u choose an image then when u click next on first activity it takes u to activity num. 2 and it shows the image u've choosen there in the imageview, and when u click back then re-click next on the first activity it takes u to activity num.2 with the image u have choosen before

How do I make the imageview gets recycled when u click the button back on the activity num. 2 so it free memory when u choose a new bitmap in the first activity so I avoid OutOfMemory problem

    // the back button
    Back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent back = new Intent(Second.this, StartActivity.class);
            startActivity(back);
        }
    });

and this is the imageview

    iv = (ImageView) findViewById(R.id.choosen_iv);
    iv.setImageBitmap(choosenBitmap);

I tried choosenBitmap.recycle(); but it doesn't work, when I click next on the first activity the bitmap is still there


Solution

  • Rather than starting your StartActivity again in the onClick method for your back button, just call finish() for the Second activity. That will take you back to the first activity. Then when you start the second activity again (by selecting another bitmap), it will initialize everything like it did the first time through.