Search code examples
androidandroid-volleyandroid-module

How to access Android module application context


I have created an android module(module project) that helps to access HTTP calls (Http Module wrap volley). I want to make Volley.newRequestQueue(mContext); into a place that initialize once rather than creating every time (To avoid memory overflows). A better place would be the Application class but from the module, I do not want to access the application. Is there any place I can initialize volley requestQue once and then use it. Was there a component like application in the module?


Solution

  • I did create a singleton class in my module to get the request

    public class RequestQueSingleton {
        private static RequestQueSingleton sSoleInstance;
        private static RequestQueue reQuestQue;
    
        private RequestQueSingleton(){}  //private constructor.
    
        public static RequestQueSingleton getInstance(Context context){
            if (sSoleInstance == null){ //if there is no instance available... create new one
                sSoleInstance = new RequestQueSingleton();
                reQuestQue = Volley.newRequestQueue(context);
            }
    
            return sSoleInstance;
        }
    
    
        public  synchronized RequestQueue getInstance() {
            Log.d("Request Que Obj",reQuestQue.hashCode()+"");
            return reQuestQue;
        }
    }