I get a lack of memory condition when I use an array adapter and change the screen orientation. I made the smallest fully working code that shows this problem.
package com.foobar.test; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class TestAppActivity extends Activity { protected ArrayList<ListItem> mModel = new ArrayList<ListItem>(); protected ListAdapter mAdapter = null; private static Context mApplicationContext = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.gc(); setContentView(R.layout.main); if (mApplicationContext == null) { mApplicationContext = getApplicationContext(); } ListView lv = (ListView) findViewById(R.id.ListList); lv.setItemsCanFocus(true); mAdapter = new ListAdapter(mApplicationContext, lv, mModel); lv.setAdapter(mAdapter); if (savedInstanceState == null) { Log.i("test", "@@ start"); new Thread(new Runnable() { @Override public void run() { try { boolean change = true; Thread.sleep(4 * 1000); for (int ii = 0; ii < 200; ii++) { setRequestedOrientation(change ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Thread.sleep(4 * 1000); change = !change; Log.i("test", "@@ change: " + (ii+1)); } } catch (Exception e) { e.printStackTrace(); } finally { Log.i("test", "@@ complete!"); } } }).start(); } } @Override protected void onDestroy() { super.onDestroy();
After about 70 orientation changes, I get an error from memory (because I have a particularly large background image). My XML looks like this:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/background"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android:id="@+id/ListList" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:choiceMode="singleChoice" android:cacheColorHint="#00000000" android:dividerHeight="3dp" android:overScrollFooter="@null" /> </LinearLayout>
And my simple background looks like this:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal"> </LinearLayout>
It drove me crazy because it is incredibly simple and it looks like it should work; Is this an error in the adapter code?
TIA.
I simplified my code even more:
package com.foobar.test; import java.util.ArrayList; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; public class TestAppActivity extends Activity { protected ArrayList<ListItem> mModel = new ArrayList<ListItem>(); protected ListAdapter mAdapter = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv = (ListView) findViewById(R.id.ListList); lv.setItemsCanFocus(true); mAdapter = new ListAdapter(this, lv, mModel); lv.setAdapter(mAdapter); } @Override protected void onDestroy() { super.onDestroy(); View root = findViewById(R.id.RootView); ((BitmapDrawable) root.getBackground()).getBitmap().recycle();
If I run this code, then manually change the screen orientation 20 times, and then go to the Eclipse Memory Analyzer Tool, it shows 20 instances of my activity.
Interestingly, if I do this on a 3.x device, I do not have this problem - I assume it was fixed in Honeycomb?