strong references to views in an activity/fragment and garbage collection

427 views Asked by At

In an activity, I typically keep around strong references to views in order to not need to findViewById(...) each time I have to update said view. In doing so, Activities end up looking like this:

SomeActivity extends Activity implements View.OnClickListener{

    private Button button;
    private TextView textView;    

    @Override
    public void onCreate(Bundle sIS){
        super.onCreate(sIS);

        this.button = (Button)findViewById(R.id.button);
        this.button.setOnClickListener(this);

        this.textView = (TextView)findViewById(R.id.
    }

    @Override
    public void onClick(View v){
        ...
    }
    ...
}

My question is, do these strong references to views with callbacks have to be nulled, or have their OnClickListeners set to null? Or is the GC able to see that this whole hierarchy is dead and GC's the whole thing?

1

There are 1 answers

2
CommonsWare On

My question is, do these strong references to views with callbacks have to be nulled, or have their OnClickListeners set to null?

No.

Or is the GC able to see that this whole hierarchy is dead and GC's the whole thing?

Yes, assuming nothing else has references to anything here (background threads, static data members, etc.).