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, AsyncTask
s 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 BroadcastReceiver
s. 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 BroadcastReceiver
s show up after the AsyncTask
s have completed.
The questions then are,
- Do
IntentService
s block UI by nature, likeAsyncTask
get()
? The PoC answer is NO, but in my real-time app, interim progress update fails. - How can an
AsyncTask
wait till anIntentService
has finished? - Why are logs in the
BroadcastReceiver
displayed only after theAsyncTask
s 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!