Passing Activity Context to constructors to use internally - is this bad

jax picture jax · May 26, 2010 · Viewed 13.3k times · Source

Is it bad practice to pass the Context to a constructor and save it as a private variable for internal use? The other option is to pass the Context as a parameter to methods that need it.

Which is a better option? I have a feeling that passing to the constructor might result in memory leaks accidentally.

Answer

JRL picture JRL · May 26, 2010

Often, all you need is the ApplicationContext, so what you can do is pass this.getApplicationContext() instead of just this. Your app context exists for the lifetime of the app anyway, so it's not a memory leak.