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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to