What is the use of the singleton class in volleyball?

I used the Volley library to upload an image using the Singleton class.
Problem:

If I used a plain-colored class, I could successfully load the image at a time, and I also noticed that the image was loaded successfully without using the plain-colored class.

Could you tell me what are the advantages of the singleton class in my code?

--------------------- Code with class Singleton --------------------

MainActivity.java

public class MainActivity extends AppCompatActivity { Button response_click; TextView text_response; RequestQueue requestQueue; String server_url="http://i.imgur.com/7spzG.png"; ImageView imageView; ImageRequest imageRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); response_click=(Button) findViewById(R.id.click_response); text_response=(TextView) findViewById(R.id.text_response); imageView=(ImageView) findViewById(R.id.image_download); }//onCreate Ending public void response_click(View view){ final ImageRequest imageRequest=new ImageRequest(server_url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { imageView.setImageBitmap(response); } }, 0, 0, null, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(),"You Got an Error....",Toast.LENGTH_LONG).show(); } }); MySingleTon.getInstance(MainActivity.this).addToRequestQue(imageRequest); }//Button Click Ending }//Main Activity Ending 

MySingleTon.java

 public class MySingleTon { private static MySingleTon mySingleTon; private RequestQueue requestQueue; private static Context mctx; private MySingleTon(Context context){ this.mctx=context; this.requestQueue=getRequestQueue(); } public RequestQueue getRequestQueue(){ if (requestQueue==null){ requestQueue= Volley.newRequestQueue(mctx.getApplicationContext()); } return requestQueue; } public static synchronized MySingleTon getInstance(Context context){ if (mySingleTon==null){ mySingleTon=new MySingleTon(context); } return mySingleTon; } public<T> void addToRequestQue(Request<T> request){ requestQueue.add(request); } } 

Here I am writing code to upload an image without using the Singleton class. Please check this code also.
One thing to keep in mind here: without using the Singleton class, I also did my job without errors.

--- --------------------- Code Without the Singleton class ---------------------- -

MainActivity.java

 public class MainActivity extends AppCompatActivity { Button response_click; TextView text_response; RequestQueue requestQueue; String server_url="http://i.imgur.com/7spzG.png"; ImageView imageView; ImageRequest imageRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); response_click=(Button) findViewById(R.id.click_response); text_response=(TextView) findViewById(R.id.text_response); imageView=(ImageView) findViewById(R.id.image_download); }//onCreate Ending public void response_click(View view){ requestQueue=Volley.newRequestQueue(this.getApplicationContext()); final ImageRequest imageRequest=new ImageRequest(server_url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { imageView.setImageBitmap(response); } }, 0, 0, null, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(),"You Got an Error....",Toast.LENGTH_LONG).show(); } }); requestQueue.add(imageRequest); }//Button Click Ending }//Main Activity Ending 
+6
source share
1 answer

If your application is constantly using the network, it is probably most efficient to configure one instance of RequestQueue that will run for the life of your application. You can achieve this in various ways. The recommended approach is to implement a singleton class that encapsulates RequestQueue and other Volley functionality. Another approach is to subclass Application and set up RequestQueue in Application.onCreate (). But this approach is not recommended; a static singleton can provide the same functionality in a more modular way.

The key concept is that RequestQueue should be created with the application context, and not with the action context. This ensures that the RequestQueue lasts for the entire life cycle of your application, and is not recreated every time the action is recreated (for example, when the user rotates the device).

Read the documentation https://developer.android.com/training/volley/requestqueue.html#singleton

+6
source

Source: https://habr.com/ru/post/1261188/


All Articles