Created
May 25, 2014 22:45
-
-
Save cesarferreira/f14c91f0426f50712f1d to your computer and use it in GitHub Desktop.
An example of a Singleton made for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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