title: Android background service state information
body: <p>I have an activity that controls starting and stopping of a background service via. a button click. When the service is not running the button says 'START SERVICE' and when the service is running the button says 'STOP SERVICE'. The logic, what should happen on button click, toggles in the same manner (if the service is not running we start it and bind to it on button click and we stop it and unbind from it if the service is running). </p>
  
  <p>Please, take a look at the code excerpt bellow for more information.</p>
  
  <p><strong>Bind and unbind</strong></p>
  
  <p>While the service can currently only be stopped through the STOP SERVICE button, I'm unbinding the service in onPause and rebinding it in onResume. Does this make sense? onPause and onResume lifecycle methods are called quite often, so this is a lot of binding and unbinding overhead, but as onPause is always called before the application could be killed, I think this is the best place for releasing service bindings and to avoid leaks.</p>
  
  <p><strong>BACK button</strong></p>
  
  <p>I'm also not sure what to do when the BACK button is pressed. Should the service remain running in the background when the BACK button is pressed (the same behaviour as in Android Music application)? If yes, what would be the proper way to persist the information that the service is running (as <em>onSaveInstanceState</em> is not getting called on BACK button pressed I am wondering what would be the cleanest way to persist the application's state - in my case the isServiceRunning boolean variable)? I need this information to set the correct text on the start/stop button (with respect to the state of the background service) when the activity will be started again. One possible alternative would also be to allow the activity to finish normally without saving any service state information (isServiceRunning). I could get this information in onCreate from ActivityManager, where I could query for the running services. </p>
  
  <p>On the other hand, if the service should be stopped on the BACK button press, where should I implement stopping of the service? onDestroy would be a natural answer to this question, but I'm facing problems as onDestroy is also getting called on activity restart (e.g. on orientation change) and I do not want to stop the service in that case.</p>
  
  <pre><code>    public class ServiceController extends Activity {
   public void onStartClick(View v) {
    if (!isServiceRunning) {
     button.setText("STOP");
     isServiceRunning = true;
     startService(...)
     bindService(...)
    } else {
     button.setText("START");
     isServiceRunning = false;
     unbindService(...)
     stopService(...)
    }
   }
  
   @Override
   protected void onResume() {
    super.onResume();
    if (isServiceRunning) {
     button.setText("STOP");
     bindService(...);
    }
   }
  
   @Override
   protected void onPause() {
    super.onPause();
    if (mBoundAccService != null) {
     unbindService(...);
    }
   }
  
   @Override
   protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) {   
     isServiceRunning = savedInstanceState.getBoolean(ID_SERVICE_RUNNING);
    }
   }
  
   @Override
   protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(ID_SERVICE_RUNNING, isServiceRunning);
   }
      } 
  </code></pre>