I have extended my Application in my Android App in order to store the different typefaces I use within my application. Everything is working fine, however I am not understanding the singleton part of the code and what is the goal of the getInstance (I set a breakpoint there and it is never called, also if I change the name of the method the Application works fine as well). Could someone enlighten me about this pattern? I don't normally work with OOP so it might be a stupid question.
MyApplication.java
public class MyApplication extends Application {
private Typeface _typefaceNormal;
private Typeface _typefaceBold;
private static MyApplication singleton;
public MyApplication getInstance() {
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
singleton = this;
this._typefaceNormal = Typeface.createFromAsset(getAssets(),"PTS55F.TTF");
this._typefaceBold = Typeface.createFromAsset(getAssets(), "PTS75F.TTF");
}
public Typeface getNormalFont() {
return this._typefaceNormal;
// return this._typefaceNormal;
}
public Typeface getBoldFont() {
return this._typefaceBold;
// return this._typefaceBold;
}
}
And then I make the call like this:
this._typeface = ((MyApplication) _activity.getApplicationContext()).getNormalFont();
EDIT
From the answers I gathered, and some additional research, this was the class I ended up using:
public class MyGlobalConfig {
private static MyGlobalConfig singleton;
private Typeface _typefaceNormal;
private Typeface _typefaceBold;
private MyGlobalConfig() {
}
public static MyGlobalConfig getInstance() {
if (singleton == null) {
singleton = new MyGlobalConfig();
}
return singleton;
}
public void init(Context ctx, String typefaceNormal, String typefaceBold) {
this._typefaceNormal = Typeface.createFromAsset(ctx.getAssets(),
typefaceNormal);
this._typefaceBold = Typeface.createFromAsset(ctx.getAssets(),
typefaceBold);
}
public Typeface getNormalFont() {
return this._typefaceNormal;
// return this._typefaceNormal;
}
public Typeface getBoldFont() {
return this._typefaceBold;
// return this._typefaceBold;
}
}
I initialize it as:
MyGlobalConfig.getInstance().init(getApplicationContext(), normalFont, boldFont);
And set my Fonts as:
MyGlobalConfig.getInstance().getNormalFont();