Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Register a <code>BroadcastReceiver</code> in the fragments you have and in its <code>onReceive</code> call <code>refresh</code> - this method is supposed to update the UI depending what you have inside. For making your code easy to use, have a base <code>Fragment</code> class and do the registering/unregistering there along with an abstract <code>refresh</code> method that will be implemented by children fragments. Something like:</p> <pre><code>public abstract class BaseRefreshableFragment extends Fragment { private BroadcastReceiver refreshReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if ("package_name.REFRESH_ACTION".equals(intent)) { refresh(); } } }; public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); IntentFilter filter = new IntentFilter("package_name.REFRESH_ACTION"); LocalBroadcastManager.getInstance(getActivity()).registerReceiver(refreshReceiver, filter); } @Override public void onDestroyView() { LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(refreshReceiver); super.onDestroyView(); } protected abstract void refresh(); } </code></pre> <p>In your <code>Service</code>, when your work is done, broadcast an intent with above action. So if there are fragments to show updated data, their receiver will be notified and that will call <code>refresh</code> <em>ON EACH FRAGMENT</em>. Something like from your service:</p> <pre><code>Intent intent = new Intent("package_name.REFRESH_ACTION"); LocalBroadcastManager.getInstance(MySyncService.this).sendBroadcast(intent); </code></pre> <p>The advantage is that you don't need to care when the fragment is shown or not since the receiver is there for the life-time of your fragment's view.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload