recyling bitmaps in OnDestroy - 'trying to use a recycled bitmap' when relaunching activity
<p>I have two simple activities <code>A</code> and <code>B</code>. The user starts <code>B</code> from <code>A</code> by pressing a button, and the user then returns to <code>A</code> by pressing the back button.</p>

<p>In the <code>onDestroy()</code> method of activity <code>B</code> I recycle some background images used in activity <code>B</code>. What I am trying to understand is why, when activity <code>B</code> is started again, I am getting 'trying to use a recycled bitmap'. Surely the bitmaps will be loaded again in the onCreate() method? like they must have been on the first time the activity was launched.</p>

<p>Here is my example code:</p>

<pre><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();
     }
 }
</code></pre>

<p>The code I use to launch activity <code>B</code> from <code>A</code></p>

<pre><code>     Intent intent = new Intent(ActivityA.this, ActivityB.class);
     startActivity(intent);
</code></pre>

<p>selectionpage.XML : </p>

<pre><code>&lt;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"&gt;

        &lt;ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/selectionimage"/&gt;
        &lt;/LinearLayout&gt;                
</code></pre>

<p>This part may be relevant. I'm not sure. I've noticed that after launching activity B, even after it has been destroyed, I can still see an instance of my activity when analyzing the memory heap using MAT. The path to GC roots seems to go through Java.lang.Thread and ContextImpl.</p>
