Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volley Singleton & Image Loader #1

Open
sondosaabed opened this issue Jan 5, 2024 · 0 comments
Open

Volley Singleton & Image Loader #1

sondosaabed opened this issue Jan 5, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@sondosaabed
Copy link
Owner


RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()).
    getRequestQueue();

MySingleton.getInstance(this).addToRequestQueue(stringRequest);

https://github.com/CypressNorth/Volley-Singleton/blob/master/VolleySingleton.java

https://google.github.io/volley/requestqueue.html

`public class MySingleton {
private static MySingleton instance;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private static Context ctx;

private MySingleton(Context context) {
    ctx = context;
    requestQueue = getRequestQueue();

    imageLoader = new ImageLoader(requestQueue,
            new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap>
                cache = new LruCache<String, Bitmap>(20);

        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            cache.put(url, bitmap);
        }
    });
}

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

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
    }
    return requestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return imageLoader;
}

}

@sondosaabed sondosaabed self-assigned this Jan 5, 2024
@sondosaabed sondosaabed added the enhancement New feature or request label Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant