Sunday, 11 August 2013

Is my custom Application object instantiated before any of my other code is executed?

Is my custom Application object instantiated before any of my other code
is executed?

In my Application subclass, I save a static reference to Context, so that
I don't have to pass contexts around in the rest of my code. I just use
MyApplication.getAppContext() whenever I need the application Context:
public class MyApplication extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getAppContext() {
return context;
}
}
Is this safe? Is there a possible scenario where
MyApplication.getAppContext() returns null?

No comments:

Post a Comment