Update a recyclerView in another activity from async thread of other activity
<p>I have two activity A and B.</p>

<p>In activity A I have a AsyncHttpClient fetching some data from a website. Once the data is received, I refresh the RecyclerView in activity A. But while the data is being fetched if someone switches to activity B, i want the RecyclerView of activity B to be refreshed on completion of data fetch.</p>

<p>Currently I am able to update in the same activity A, but can't do that in B.</p>

<p>Here is what I have done - </p>

<pre><code>public void loadFromWeb(){
            RequestParams params = new RequestParams();
            AsyncHttpClient client = new AsyncHttpClient();
            params.put("id", id);

            client.post("http://example.com/process.php", params, new JsonHttpResponseHandler() {
                @Override
                public void onStart() {

                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    try {
                        //process the response

                        adapterInActivityA.notifyDataSetChanged();
                        //This works for Activity A

                        //How to implement the function updateInActivityB()?  
                        updateInActivityB();

                    } catch (Exception e) {
                        //catch exception
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    // Process failure
                }

            });
}
</code></pre>

<p>The problem is how to implement function updateInActivityB()</p>

<hr>

<h2>Solution (As suggested by Alok)</h2>

<p>Following are the details of what I did for others who want to do the same - </p>

<p>This uses <a href="https://github.com/greenrobot/EventBus" rel="nofollow">greenrobot's EventBus library</a></p>

<p>Compile the library by adding it to the app's build.gradle file</p>

<p>Compile the library</p>

<pre><code>compile 'de.greenrobot:eventbus:2.4.0'
</code></pre>

<p>Create your event class, this will store the data which you are communicating.</p>

<pre><code>//In FirstActivity

public class myEvent {
    private final String data;

    //Constructor
    public myEvent(String data){
        this.data = data;
    }

    public String getData(){
        return data;
    }
}
</code></pre>

<p>Whenever you want to send an update the data has been downloaded (i.e. in updateInSecondActivity() function) </p>

<p><strong>post the update -</strong></p>

<pre><code>//In FirstActivity

public void updateInSecondActivity(){
    EventBus.getDefault().post(new myEvent("Updated Data");
}
</code></pre>

<p>Now, In the SecondActivity, you have to listen for this event. First Register in the SecondActivity to subscribe to the events -</p>

<p><strong>Register for the events</strong></p>

<pre><code>//In SecondActivity

EventBus.getDefault().register(this);
</code></pre>

<p>You can register in onCreate(), onStart(), onResume(). Also don't forget to unregister in onPause(), onDestroy(), onStop() -</p>

<p><strong>Unregister the event listener</strong></p>

<pre><code>//In SecondActivity

EventBus.getDefault().unregister(this);
</code></pre>

<p>Once the registration and unregistration is in place, you have to listen if the update has been posted by FirstActivity or not. You can do this by creating an onEvent() function which listens for posting of myEvent class -</p>

<p><strong>Listen to the event</strong></p>

<pre><code>//In SecondActivity

public void onEvent(myEvent mE){
    String updatedData = me.getData();
    // Process/update using the updateData
}
</code></pre>