If I have a Helper Class like the following:
public class TestHelper {
private Context context;
public TestHelper(Context context);
this.context = context;
}
public doSomethingWithContext(){
//some code
}
and my Activity looks like the following:
public class MainActivity extends AppCompatActivity{
private TestHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
helper = new TestHelper(this);
}
}
Will the context be leaked like this or do I have to set helper to null in the onDestroy method (would this even work for the GC)?
Also I need the helper in several methods (onCreate, onPause etc) so creating the Helper inside in all of the methods doesn't sound like a good solution.
Context doesn't leak in your code because GC correctly handles cyclic references. See explanation here
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments