Boo Boo - 2 months ago 4x
Java Question

updating UI with a volley request from a different class

I am having trouble with volley (again). I want to run a volley request in a class, so more than one activity can feed off its results and update their UI's accordingly. I have got it return data and call the request from the UI but now im struggling to update the UI with the new data. I have looked at answers but I'm trying to understand the structure and I am at a loss, can some please advise/ talk me through it


assuming I understand what you mean as being:

A Volley request returns, updates some data set through some activity

In this case, assuming the calling activity contains everything, and reminding that this is a very general example, what you should usually do (usually, since there are exceptions to the case), is just insert the data into the data set contained in your UI holder (e.g. your recycler adapter) and update it, an example would be your adapter holding a method similar to this:

public void updateDataSet(List<Item> items)
    //mItemList is the adapters member list
    if (null != mItemList)
        mItemList = items;


you call this inside the request callback you fired earlier, just make sure to initialize everything BEFORE you fire the request, e.g.

 public void onResponse(JSONObject response)
     Log.d(TAG + ": ", "somePostRequest Response : " + response.toString());
     // here you need to parse to JSON to a list and then call...
     List<Item> items = parseResponse(response);

Now, if what you meant was

A Volley request returns in some Activity, I want it to update stuff in another place

there are a couple of options:

  1. As someone said in the comments - you could go for EventBus.
  2. You could hold a DataManager class, which would be a global singleton, in which case you can either hold the data and update it there, and then every activity (in it's onResume or other relevant lifecycle method) knows to pull that data.
  3. You could do the same as option 2, with the exception of that DataManager holding a reference to other UI parts (e.g. Fragments), and triggering member methods in them that pass the data and trigger the updates.

Personally I find option 3 cumbersome and somewhat bad practice, but if all else fails, (and it shouldn't, but if it does) then you can try.

There are more options out there, it depends and varies according to the data, your app architecture, coding style and other stuff you apply.

Hope this helps!