Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What happens if the user presses BACK or HOME after entering in the "data of a 'problem'", before the "solution" is ready?</p> <p>If the answer is that you do not need to do anything, then get rid of the <code>Service</code>, and have the <code>Activity</code> do the work to compute the solution (perhaps using an <code>AsyncTask</code>). You are not gaining anything in this case by using a <code>Service</code>.</p> <p>If, however, the user will still want the "solution", then using a <code>Service</code> is fine, but you have to take into account that the <code>Activity</code> that started the service may no longer exist or may not be in the foreground.</p> <p>One way to handle this is to use an ordered broadcast. Have the <code>Activity</code> register a high-priority <code>BroadcastReceiver</code> for the broadcast when it is in the foreground (via <code>onResume()</code> and <code>onPause()</code>), and register a low-priority <code>BroadcastReceiver</code> for the same broadcast via the manifest. The <code>Service</code> would <code>sendOrderedBroadcast()</code> when its work is complete. The <code>Activity</code>-registered <code>BroadcastReceiver</code> would update its UI with the result and abort the broadcast. The manifest-registered <code>BroadcastReceiver</code> would raise a <code>Notification</code> to let the user know about the completed work. <a href="http://commonsware.com/blog/2010/08/11/activity-notification-ordered-broadcast.html" rel="nofollow">Here is a blog post</a> with more about this pattern, and <a href="https://github.com/commonsguy/cw-advandroid/tree/master/Broadcast/Ordered" rel="nofollow">here is a sample project</a> that demonstrates its use.</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