Skip to content

Instantly share code, notes, and snippets.

@cesarferreira
Created May 25, 2014 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarferreira/f14c91f0426f50712f1d to your computer and use it in GitHub Desktop.
Save cesarferreira/f14c91f0426f50712f1d to your computer and use it in GitHub Desktop.
An example of a Singleton made for Android
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String limitedResource = Utils.getInstance(getApplicationContext()).limitedResource;
Log.d("stuf", limitedResource);
}
}
public class Utils {
private static Utils mInstance = null;
private Context mContext;
public String limitedResource = "hello world";
private Utils(Context context) {
mContext = context;
}
public static Utils getInstance(Context context) {
if (mInstance == null) {
Log.d("tag", "############## INITING UTILS ############ ");
mInstance = new Utils(context);
}
return mInstance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment