John Jared
John Jared

Reputation: 800

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

Upvotes: 0

Views: 1002

Answers (2)

yugidroid
yugidroid

Reputation: 6690

Why do you need a back button with an Intent to go back to the previous activity? The hardware back button can deal with that by itself.

You can use the onDestroy() activity's lifecycle method to recycle your choosenBitmap Bitmap type variable.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234795

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.

Upvotes: 2

Related Questions