android


Where exactly singleton used in android? [closed]


As am very new to Android I've red a lot about singleton classes in java. I completely understand the concept of singleton, but I'm really confused where to use it in Android, I have used it neither yet.Could you guide or explain me with a real example where to use or in which case should I use a singleton class?
It's depend on your projects and how you implement your MVC or MVP structure. You can use any design pattern in your code and your classes where you need. This is an example of singleton class:
public class Singleton {
private static Singleton mInstance = null;
private String mString;
private Singleton(){
mString = "Hi!";
}
public static Singleton getInstance(){
if(mInstance == null)
{
mInstance = new Singleton();
}
return mInstance;
}
public String getString(){
return this.mString;
}
public void setString(String value){
mString = value;
}
This class create once and when you need an object of this class just use object was created before
Take a look at this gist : https://gist.github.com/Akayh/5566992
it works but it was done very quickly :
MyActivity : set the singleton for the first time + initialize mString attribute ("Hello") in private constructor and show the value ("Hello")
Set new value to mString : "Singleton"
Launch activityB and show the mString value. "Singleton" appears...
It totally depends on your need and requirement thought we use it in almost every project to make our life easier.
In android I use singleton of DatabaseHelper so that I don't have to be extra careful about about the opening and closing the database again and again and dealing with the database lock and concurrent accesses of the database etc.
Check this :-http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html
2.Then I use to manage SharedPreferences to store the sharedpreferences it will be a lot easier to maintain the code and I don't I have to go around and around to see check the keys value and all.
Check this :-https://medium.com/#ali.muzaffar/android-sharedpreferences-singleton-to-make-life-easier-f1d802b6cd8e.
3.Then I use it when I want to access an object anywhere in the application and which is frequently required.
Hope you have got the little bit idea where to use.
most use in Volley API When you want one object from Volley in your app
ex: for Singleton this example explain you get one RequestQueue in your App
public class VolleySingleton {
private static VolleySingleton singleton;
private RequestQueue requestQueue;
private VolleySingleton(){
requestQueue= Volley.newRequestQueue(MyApplication.getAppContext());
}
public static VolleySingleton getInstance(){
if(singleton==null)
singleton=new VolleySingleton();
return singleton;
}
public void setRequestQueue(RequestQueue requestQueue)
{
this.requestQueue = requestQueue;
}
public RequestQueue getRequestQueue() {
return requestQueue;
}
}

Related Links

android asynchronous interaction with server
android login page design/creation [closed]
Update Inbox style Notification Like gmail
IBM Worklight - How to make Android environment code “independent” from the generated project?
Download different types of file from url
android sliding menu library not able to extend menu width dynamically
How to list and number text file contents in batch window and allow user selection?
How can i display googleplay application list in my own application?
How to target ads for location in admon
Android Getting Data From Another Class [duplicate]
cancelling AsyncTask and it's progressbar from a handler
Android: Receive HTML page through intent
Fragments inside a fragment Android application
PhoneGap for Android - There is no script engine for file extension “.js”
Remove HighCharts border for Android
How to get the current Activity context for a non-activity class, statically

Categories

HOME
oracle12c
telegram
redux-form
themes
sh
error-handling
odoo-8
mautic
jxl
qsqlquery
heat
libusb
do-while
red5
nim
connector
sencha-touch
boost-icl
solrcloud
google-maps-autocomplete
poedit
aggregation
flask-sqlalchemy
resourcebundle
xunit
fable-f#
subclass
cppreference
send
git-squash
autolisp
laravel-eloquent
github3.py
twos-complement
arduino-ide
browser-extension
numpy-broadcasting
magento-1.9.2.4
sql-server-ce
nohup
ods
azure-arm
microsoft-metro
claims
puredata
arq
oracleclient
truezip
bbpress
rxtx
reporting-services-2012
microstation
envi
tinkerpop3
neon
x-tag
wso2developerstudio
respondcms
android-wake-lock
omniauth-facebook
stream-processing
hadoop-partitioning
sklearn-pandas
createprocess
toast
directdraw
tfs-workitem
coda
rpart
batik
jdk1.7
omnifaces
user-permissions
shopizer
java-security
procedural-programming
uncaught-typeerror
metatrader5
imagefilter
cuba
grails-plugin-rabbitmq
code-duplication
kaleidoscope
time.h
css-reset
joomla3.1
iboutlet
database-create
agility.js
gobject-introspection
screen-size
cgbitmapcontextcreate
object-database
isapi-extension
openfaces
silverlight-oob
glui
sqlobject
contentpresenter
leader
commercial-application

Resources

Encrypt Message