Is it a good practice to create a reference to application context and use it anywhere?
Posted
by kknight
on Stack Overflow
See other posts from Stack Overflow
or by kknight
Published on 2010-06-07T23:14:14Z
Indexed on
2010/06/07
23:42 UTC
Read the original article
Hit count: 198
android
I have to use context in many places of my code such as database operations, preference operations, etc. I don't want to pass in context for every method.
Is it a good practice to create a reference to application context at the main Activity and use it anywhere such as database operations? So, I don't need some many context in method parameters, and the code can avoid position memory leak due to use of Activity Context.
public class MainActivity extends Activity {
public static Context s_appContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
s_appContext = this.getApplicationContext();
© Stack Overflow or respective owner