• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Calling Singleton from Fragment

How to call a singleton class from fragment in android studio?


This is my code.
MySingleton is the class here.

MySingleton.getInstance(MapsFragment.this).addToRequestque(jsObjRequest);


The compiler shows "get instance in MySingleton cannot be applied to MapsFragment.

How to solve this?
 
Move to Application Development Forum.

Please show the code for class MySingleton.
 
import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;


public class MySingleton {

private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mCtx;

private MySingleton(Context context){
mCtx = context;
requestQueue = getRequestQueue();
}


public RequestQueue getRequestQueue(){
if(requestQueue==null){
requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());

}
return requestQueue;
}
public static synchronized MySingleton getInstance(Context context)
{
if(mInstance==null)
{
mInstance = new MySingleton(context);
}
return mInstance;
}

public<T> void addToRequestque(Request<T> request){
requestQueue.add(request);
}
}
 
Back
Top Bottom