Keeping a service running during orientation changes
<p>In my application, I have a <code>Service</code> which is responsible for looking after a Bluetooth connection to an external device. This <code>Service</code> class periodically polls the external Bluetooth device for data and adds that latest data into a log held in cache (or possibly SD card) memory. </p>

<p>Amongst the various <code>Activity</code> classes that I have, there is one particular <code>Activity</code> that represents the main UI. It is responsible for displaying, in graphical form, the logged data based upon the cache file data. Let's call this <code>Activity</code> the <code>Dashboard</code>. The user can scroll back and forth on that graph to review the data collected and logged within the cache since the application was started. </p>

<p>For the purpose of this question, there are two modes of operation to consider. It is possible for the user to select a "log to SD card" option, whereby the application must continue the polling and logging to SD card even when all <code>Activity</code> classes are killed' (e.g. the user has gone back to the launcher). In this case my <code>Service</code> is started using <code>.startService()</code> and continues to run, and will only be stopped when the user invokes the application again and disabled SD card logging. The other mode is where the user hasn't selected "log to SD card", in which case the <code>Service</code> is still managing the Bluetooth connection, polling and logging to cache memory for the purpose of visually displaying the data on the graph, but only needs to do so while the <code>Dashboard</code> <code>Activity</code> is being used. </p>

<p>What I have at the moment is that the <code>Dashboard</code> <code>Activity</code> initially binds to the <code>Service</code> using <code>bindService()</code>, and does a corresponding call to <code>unbindService()</code> within the <code>onPause()</code> method (as otherwise I would of course leak the <code>Service</code>).</p>

<p>The problem is that the <code>Service</code> needs to maintain the Bluetooth connection and continue logging during orientation changes or when the user invokes another <code>Activity</code> over the top (e.g. checks an email). Now if the user has selected "log to SD card" resulting in a call to <code>startService()</code> then of course there is no problem. The problem of course is how to distinguish between an <code>Activity</code> being destroyed and then created again due to orientation (or some other configuration) change, and being destroyed because the user went back to the launcher. In the former case, I don't want the <code>Service</code> datalogging to have been interrupted. In the latter case, I want the <code>Service</code> to stop, <em>if</em> the user hasn't selected "log to SD card". </p>

<p>The best solution I can think of for this at the moment is for the service to be <em>always</em> started using <code>startService()</code>, so that it continues to run when the <code>Dashboard</code> has been destroyed. What I would then do is implement a time-out within the <code>Service</code>, whereby the <code>Service</code> will stop itself <em>unless</em> continuous SD card logging is enabled, <strong>or</strong> the <code>Dashboard</code> is <code>onCreate</code>d again within five seconds (say) and re-binds to the <code>Service</code>. This seems a little crude, and I can't help thinking this must be a common design problem that has a better solution that I've overlooked. </p>
