AndroidKing AndroidKing - 1 year ago 57
Java Question

What is the procedure to create arraylist which should store data in singleton class(Android)

I am using arraylist in my application. I would like to know exact procedure to initialize ArrayList from Singleton class. And the data will be used in some other activities.

Can anybody help to know about Singleton class.

Thanks in advance

Answer Source

Here is how to create your singleton class :

    public class YourSingleton  {  

        private static YourSingleton mInstance;
        private ArrayList<String> list = null;

        public static YourSingleton getInstance() {
            if(mInstance == null)
                mInstance = new YourSingleton();

            return mInstance;

        private YourSingleton() {
          list = new ArrayList<String>();
        // retrieve array from anywhere
        public ArrayList<String> getArray() {
         return this.list;
        //Add element to array
        public void addToArray(String value) {

Anywhere you need to call your arrayList just do :


To add elements to array use :

 YourSingleton.getInstance().addToArray("first value");