You can use broadcast receiver for your activity. You can send a broadcast and if your activity is 'alive' and is registered to receive the broadcast you can trigger the recyclerView update from there.
Send the broadcast when you are creating the notification like this
Intent intent = new Intent("key_to_identify_the_broadcast");
Bundle bundle = new Bundle();
bundle.putBoolean("updateRecyclerView",true);
intent.putExtra("bundle_key_for_intent", bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
and in your Activity where you want to receive this intent, you can use a broadcast receiver
private final BroadcastReceiver mHandleMessageReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle =
intent.getExtras().getBundle("bundle_key_for_intent");
if(bundle!=null){
boolean shouldRefresh = bundle.getBoolean("updateRecyclerView");
if(shouldRefresh){
//Refresh your recyclerView
}
}
}
};
You need to register and unregister the receiver to work
In your onResume method you can register this receiver to receive the broadcast
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter filter = new IntentFilter("key_to_identify_the_broadcast");
LocalBroadcastManager.getInstance(this)
.registerReceiver(mHandleMessageReceiver,filter);
}
You also need to unregister it before your activity gets paused
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try {
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mHandleMessageReceiver);
} catch (Exception e) {
Log.e("UnRegister Error", "> " + e.getMessage());
}
}