I have two simple actions A
and B
The user starts B
with A
by pressing a button, and then the user returns to A
by pressing the back button.
In the onDestroy()
method of activity B
I reuse the background images used in activity B
I am trying to understand why, when activity B
starts up again, I get an "attempt to use a recycled raster map". Of course, the bitmaps will be loaded again in the onCreate () method? as if they were the first time the activity was launched.
Here is my sample code:
public class ActivityB extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.selectionpage); } @Override public void onDestroy() { ImageView iv = (ImageView) findViewById(R.id.imageView1); ((BitmapDrawable)iv.getDrawable()).getBitmap().recycle(); LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1); ((BitmapDrawable)ll.getBackground()).getBitmap().recycle(); super.onDestroy(); } }
The code I use to start activity B
from A
Intent intent = new Intent(ActivityA.this, ActivityB.class); startActivity(intent);
selectionpage.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/backgroundimage"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/selectionimage"/> </LinearLayout>
This part may be relevant. I'm not sure. I noticed that after starting activity B, even after its destruction, I still see an instance of my activity when analyzing a heap of memory using MAT. The path to the GC roots seems to go through Java.lang.Thread and ContextImpl.
Wozza source share