This builds upon an earlier question, which is a PoC.
The above diagram shows the basic setup of the workings in my app.
In the real-time app, I am invoking the IntentService in the onOptionsItemSelected() method of a Fragment, like this:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
...
...
Intent myServiceIntent = new Intent(getActivity(), MyService.class);
getActivity().startService(myServiceIntent);
...
...
}
MyService downloads data from the cloud and saves them into a local SQLite database. Next, AsyncTasks in the same Fragment consume the data to update another Fragment. The key is, the AsyncTask should begin their doInBackground() after MyService is done.
The PoC worked with Service well, showing the ProgressBar and keeping the UI constantly updated with intermediate results through BroadcastReceivers. Please note the Service was called from AppCompatActivity, but in the real app, its called from a Fragment. The exact same setup fails, no ProgressBar, no interim updates. Log messages from the BroadcastReceivers show up after the AsyncTasks have completed.
The questions then are,
- Do
IntentServices block UI by nature, likeAsyncTaskget()? The PoC answer is NO, but in my real-time app, interim progress update fails. - How can an
AsyncTaskwait till anIntentServicehas finished? - Why are logs in the
BroadcastReceiverdisplayed only after theAsyncTasks have finished? - This being such a common situation, is there a best practice to overcome this?
Pardon the verbosity, but hope the situation is clear. Please comment if there are ambiguities.
Please note that I've seen this answer among others, but none really suit me, unfortunately.
Many thanks in advance!
