I'm curious too, but seriously you should go with the other options you mentioned. Creating new onClickListeners with each click is NOT a good idea !
Balwinder Kaur Open Source Development Center ·T· · ·Mobile· stick together The views, opinions and statements in this email are those of the author solely in their individual capacity, and do not necessarily represent those of T-Mobile USA, Inc. On Aug 15, 7:25 pm, Dan Sherman <[email protected]> wrote: > Hey guys, > > Was curious if anyone could point our a way to avoid the following memory > leak. > > When switching layouts in a single activity, and assigning OnClickListeners > to buttons, each assignment looks to leak a bit of memory. > > Below is a quick example application that shows it happening with two > Button's and a simple function to switch which layout is currently active. > If run, you'll see the used memory jump slightly with each click between > them. > > I'm aware there's plenty of ways around it (using multiple activities for > each layout, using a single OnClickListener thats a member variable which > gets assigned to it each time), just curious as to where that extra > reference is going thats not getting collected. > > public class SetContentTest extends Activity { > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > Button btn = (Button) findViewById(R.id.btn_main); > btn.setOnClickListener(new OnClickListener() { > @Override > public void onClick(View v) { > setMode(false); > } > }); > } > > public void setMode(boolean main) { > if (main) { > setContentView(R.layout.main); > Button btn = (Button) findViewById(R.id.btn_main); > btn.setOnClickListener(new OnClickListener() { > @Override > public void onClick(View v) { > setMode(false); > } > }); > } else { > setContentView(R.layout.other); > Button btn = (Button) findViewById(R.id.btn_other); > btn.setOnClickListener(new OnClickListener() { > @Override > public void onClick(View v) { > setMode(true); > } > }); > } > } > > } > > And the layouts: > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" > android:orientation="vertical" > android:layout_width="fill_parent" > android:layout_height="fill_parent" > > > > <Button > android:id="@+id/btn_main" > android:layout_width="wrap_content" > android:layout_height="wrap_content" > android:text="Click for Other" > /> > </LinearLayout> > > ------------------ > > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" > android:orientation="vertical" > android:layout_width="fill_parent" > android:layout_height="fill_parent" > > > > <Button > android:id="@+id/btn_other" > android:layout_width="wrap_content" > android:layout_height="wrap_content" > android:text="Click for Main" > /> > </LinearLayout> > > Only difference between the layouts is the name (btn_main and btn_other) > > - Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

