Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Volley Singleton from ViewModel

I have a new Fragment with a ViewModel created by Android Studio 3.2.1.

The objective is to make a request using a Volley Singleton proposed by Google here.

The problem is that the Google Singleton pattern for a Volley Singleton depends on a context which is not present in a ViewModel directly.

How to get an application context from a ViewModel which then will be used by a Volley Singleton?

WebsiteRestApi Singleton with Volley

package com.developer.pochttp.sampledata

import android.content.Context
import android.graphics.Bitmap
import android.support.v4.util.LruCache
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.ImageLoader
import com.android.volley.toolbox.Volley

class WebsiteRestApi constructor(context: Context) {
    companion object {
        @Volatile
        private var INSTANCE: WebsiteRestApi? = null
        fun getInstance(context: Context) =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: WebsiteRestApi(context).also {
                    INSTANCE = it
                }
            }
    }
    val imageLoader: ImageLoader by lazy {

        ImageLoader(requestQueue,
            object : ImageLoader.ImageCache {
                private val cache = LruCache<String, Bitmap>(20)
                override fun getBitmap(url: String): Bitmap? {
                    return cache.get(url)
                }
                override fun putBitmap(url: String, bitmap: Bitmap) {
                    cache.put(url, bitmap)
                }
            })
    }
    val requestQueue: RequestQueue by lazy {
        // applicationContext is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        Volley.newRequestQueue(context.applicationContext)
    }
    fun <T> addToRequestQueue(req: Request<T>) {
        requestQueue.add(req)
    }
}

ViewModel

package com.developer.pochttp.ui.main

import android.arch.lifecycle.ViewModel

class MainViewModel : ViewModel() {


}
like image 435
Blazej SLEBODA Avatar asked Oct 21 '25 14:10

Blazej SLEBODA


1 Answers

Just make an Application class like :

public class MyApplication extends Application {
   private static MyApplication mInstance;
     @Override
     public void onCreate() {
       super.onCreate();
        mInstance = this;
     }
   public static synchronized MyApplication getInstance() {
      return mInstance;
   } 

  public static Context getAppContext() {
     return mInstance.getApplicationContext();
  }
} 

Manifest.xml file editing

  <application
    android:name=".MyApplication"
  ></application>

You can provide volley an Application context which is enough for volley to operate. so now

VolleySingleton.getInstance(MyApplication.getAppContext());  

can make your volley functional from any where.

like image 173
Jatin Sahgal Avatar answered Oct 23 '25 05:10

Jatin Sahgal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!