title: Is there a way for Activity to tell if the system is going to restart a Service after it has been killed by system?
body: <p>Say I have an Activity that launches a Service using <code>startService()</code> when user presses a button. The service is stopped using another intent in <code>startService()</code> when user presses another button, or it can decide to stop itself. The service wants to run forever, so it returns <code>START_STICKY</code> from <code>onStartCommand()</code>. While the service is running, it connects to some server and receives messages. Activity binds to it and, when bound, displays those messages.</p>
  
  <p>Now, when the Activity is started after an OOM kill, it doesn't know if the Service is started. One can use a static field set in <code>onStartCommand()</code> to tell if the Service is running, but that's prone to backfire sooner or later. Moreover, the Service can be started by the system <em>after</em> the activity has been launched, so using a static field is not an option at all.</p>
  
  <p>The cleanest solution I can come up with is using SharedPreferences to store the state of the service. This is going to fail to be good when the service is killed between <code>SharedPreferences.setBoolean("running", false)</code> and <code>stopSelf()</code>, but such a thing will probably occur rarely.</p>
  
  <p>Also I can try to bind to the Service at all times. This would be very ugly but still not impossible.</p>
  
  <p>Any other solutions? Maybe the very idea is wrong in some way?</p>