How do you clear the ArrayAdapter?

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(); // doesn't make any difference. // ListView lv = (ListView) findViewById(R.id.ListList); // lv.setAdapter(null); // adapter.clear(); // adapter = null; unbindDrawables(findViewById(R.id.ListList)); System.gc(); } private void unbindDrawables(View view) { if (view == null) { return; } if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } if (!(view instanceof AdapterView)) { ((ViewGroup) view).removeAllViews(); } } } public static class ListItem { public int placeholder; } public static class ListAdapter extends ArrayAdapter<ListItem> implements View.OnClickListener { public ListAdapter(Context context, ListView lv, List<ListItem> model) { super(context, android.R.layout.simple_list_item_1, model); } @Override public void onClick(View v) { } } } 

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(); // this doesn't seem to have any affect. ListView lv = (ListView) findViewById(R.id.ListList); lv.setAdapter(null); mAdapter.clear(); mAdapter = null; unbindDrawables(root); System.gc(); } private void unbindDrawables(View view) { if (view == null) { return; } if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } if (!(view instanceof AdapterView)) { ((ViewGroup) view).removeAllViews(); } } } } 

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?

+6
source share

Source: https://habr.com/ru/post/905130/


All Articles