I have two functions fetchData() and setDataUI(), in fetchData() i'm sending a request and saving the response. In setDataUI() function i'm setting adapter to bind the data's.

```
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);

        recyclerView = (RecyclerView) findViewById(R.id.image_recycler_view);
        data_list = new ArrayList<>();

        fetchData2(1);
}

```

fetchData2() to fetch data from server.

```
public void fetchData2(final int next){
        String url = Constants.URL+"image/gallery?page="+next;
        Log.d(TAG,"Data2 Url-->"+url);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    Log.d(TAG, "Response-->" + response);
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("media");
                    Log.d(TAG, "Media Array-->" + jsonArray);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                        ImageGallery imageGallery = new ImageGallery(
                                jsonObject1.getString("file"),
                                jsonObject1.getString("description"),
                                jsonObject.getInt("next"));

                        data_list.add(imageGallery);

                        Log.d(TAG, "Data List in AsyncTask-->" + data_list);
                        setDataUI();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ActivityImageGallery.this, "Server Error!!!!", Toast.LENGTH_SHORT).show();
            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }

```

then in setDataUI()

```
 private void setDataToUI() {
        gridLayoutManager = new GridLayoutManager(this,1);

        recyclerView.setLayoutManager(gridLayoutManager);

        Log.d(TAG,"Data_List-->"+data_list);

        adapter = new AdapterImageGallery(this, data_list);
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
                    fetchData2(data_list.get(data_list.size()-1).getNext());
                }
            }
        });
    }

```

Expected Result:
function setDataUI() must only start when fetchData2() finishes execution completely. So, data\_list in setDataUI() will have the values that are initialized in fetchData2().

Actual Result :
currently setDataUI() starts execution before fetchData2() completes its execution, resulting in data\_list to be empty in setDataUI().

I do get the correct response from the server but after the setDataUI() is executed.