title: Android Services UI updates after onDestroy()
body: <p>I am learning how to use services correctly and am running into an issue. I just have a test application that will start a service to increment an integer. I was able to use a messenger to update the UI as the integer increments. Furthermore I was able to set the launch mode of my original activity to singleTask, which allowed me to go back to the instance of the main activity using the pending intent in my service.</p>
  <p>The issue I am having is how do I go about reattaching a new activity (really I am focused on getting to the new UI view) to my service after the old one was destroyed (like as part of a screen rotation, exit app, etc.)?</p>
  <p>What happens is after the screen rotates or the activity is destroyed the UI when reopened is just the way it was when the application starts, but I can see that my service is still running.</p>
  <p>Below is my code for the service:</p>
  <pre><code>public class BackgroundService extends Service {
  
      private static final String TAG = &quot;BackgroundService&quot;;
      private NotificationManager notificationMgr;
      int counter;
  
      // use regular thread
      private ThreadGroup myThreads = new ThreadGroup(&quot;ServiceWorker&quot;);
  
      public void onCreate() {
          super.onCreate();
          notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          displayNotificationMessage(&quot;Background Service is running&quot;);
  
      };
      
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          
          super.onStartCommand(intent, flags, startId);
          
          int counter = intent.getExtras().getInt(&quot;counter&quot;);
          Messenger msg = (Messenger) intent.getExtras().get(&quot;msg_JIB&quot;);
          new Thread(myThreads, new ServiceWorker(counter, msg), &quot;BackgroundService&quot;)
              .start();
          
          return START_NOT_STICKY;
      }
      
      
       class ServiceWorker implements Runnable
          {
              int counter = -1;
              private Messenger msg1 = null;
              public ServiceWorker(int counter, Messenger msg) {
                  this.counter = counter;
                  this.msg1 = msg;
              }
  
              public void run() {
                  final String TAG2 = &quot;ServiceWorker:&quot; + Thread.currentThread().getId();
                  // do background processing here...
                  try {
                      while (counter&lt;100){
                          
                      
                      Message message = Message.obtain();
                      message.arg1=counter;
                      msg1.send(message);
                      counter = counter +1;
                      Thread.sleep(5000);
                      }
                      
                  } catch (Throwable e) {
                      
                      Log.v(TAG2, &quot;... sleep interrupted&quot;);
                  }
              }
          }
      
  
      private void displayNotificationMessage(String message) {
          Notification notification = new Notification(R.drawable.emo_im_winking,
                  message, System.currentTimeMillis());
  
          
          Intent i = new Intent(this, ProDroid_Android_CH11_Local_ServiceActivity.class);
          //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
  
          PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                  i, 0);
          
          
          notification.setLatestEventInfo(this, TAG, message, contentIntent);
          notification.flags |= Notification.FLAG_NO_CLEAR;
          notificationMgr.notify(0, notification);
  
      }
      
         @Override
          public void onDestroy()
          {
              Log.v(TAG, &quot;in onDestroy(). Interrupting threads and cancelling notifications&quot;);
              myThreads.interrupt();
              notificationMgr.cancelAll();
              super.onDestroy();
          }
  
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  
  }
  </code></pre>
  <p>This is the code to my main activity:</p>
  <pre><code>public class ProDroid_Android_CH11_Local_ServiceActivity extends Activity {
  
  
      private static final String TAG = &quot;MainActivity&quot;;
      private int counter = 1;
  
      Handler handler = new Handler() {
          @Override
          public void handleMessage(android.os.Message msg) {
  
              TextView tv = (TextView) findViewById(R.id.textView1);
              tv.setText(&quot;Count: &quot; + msg.arg1);
  
          };
  
      };
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      }
  
      @Override
      protected void onNewIntent(Intent intent) {
          
          super.onNewIntent(intent);
      }
  
      public void doClick(View view) {
          switch (view.getId()) {
          case R.id.startBtn:
              Log.v(TAG, &quot;Starting service... counter = &quot; + counter);
              Intent intent = new Intent(
                      ProDroid_Android_CH11_Local_ServiceActivity.this,
                      BackgroundService.class);
              intent.putExtra(&quot;counter&quot;, counter);
              intent.putExtra(&quot;msg_JIB&quot;, new Messenger(handler));
              startService(intent);
              
              break;
          case R.id.stopBtn:
              stopService();
          }
      }
  
      private void stopService() {
          Log.v(TAG, &quot;Stopping service...&quot;);
          if (stopService(new Intent(
                  ProDroid_Android_CH11_Local_ServiceActivity.this,
                  BackgroundService.class)))
              Log.v(TAG, &quot;stopService was successful&quot;);
          else
              Log.v(TAG, &quot;stopService was unsuccessful&quot;);
      }
  
      @Override
      public void onDestroy() {
          //stopService();
          super.onDestroy();
      }
  }
  </code></pre>