title: oncreate() method of service is invoked when the application is killed. Why?
body: <p>When my application starts, the service is started ( it starts a chronometer ) and then the activity is bounded to this service. When i kill the application, the onCreate() method of the service is invoked thus the chronometer restarts. Why onCreate() method is called ? I don't want my chronometer restarts, i want it to continue from where it left.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      BoundService mBoundService;
      boolean mServiceBound = false;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          final TextView timestampText = (TextView) findViewById(R.id.timestamp_text);
          Button printTimestampButton = (Button) findViewById(R.id.print_timestamp);
          Button stopServiceButon = (Button) findViewById(R.id.stop_service);
          Button startServiceButon = (Button) findViewById(R.id.start_service);
          printTimestampButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (mServiceBound) {
                      timestampText.setText(mBoundService.getTimestamp());
                  }
              }
          });
  
          stopServiceButon.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (mServiceBound) {
                      unbindService(mServiceConnection);
                      mServiceBound = false;
                  }
                  Intent intent = new Intent(MainActivity.this,
                          BoundService.class);
                  stopService(intent);
              }
          });
  
          startServiceButon.setOnClickListener(new OnClickListener(){
              @Override
              public void onClick(View v) {
                  if (!mServiceBound) {
                      Intent intent = new Intent(MainActivity.this, BoundService.class);
                      startService(intent);
                      bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
                      mServiceBound = true;
                  }
  
              }
          });
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          Log.v("mainactivity", "in onStart");
          Intent intent = new Intent(this, BoundService.class);
          startService(intent);
          bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
          mServiceBound = true;
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          if (mServiceBound) {
              unbindService(mServiceConnection);
              mServiceBound = false;
          }
      }
  
      private ServiceConnection mServiceConnection = new ServiceConnection() {
          @Override
          public void onServiceDisconnected(ComponentName name) {
              mServiceBound = false;
          }
  
          @Override
          public void onServiceConnected(ComponentName name, IBinder service) {
              MyBinder myBinder = (MyBinder) service;
              mBoundService = myBinder.getService();
              mServiceBound = true;
          }
      };
  }
  </code></pre>
  
  <p>BoundService.java</p>
  
  <pre><code>public class BoundService extends Service {
      private static String LOG_TAG = "BoundService";
      private IBinder mBinder = new MyBinder();
      private Chronometer mChronometer;
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.v(LOG_TAG, "in onCreate");
          mChronometer = new Chronometer(this);
          mChronometer.setBase(SystemClock.elapsedRealtime());
          mChronometer.start();
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          Log.v(LOG_TAG, "in onBind");
          return mBinder;
      }
  
      @Override
      public void onRebind(Intent intent) {
          Log.v(LOG_TAG, "in onRebind");
          super.onRebind(intent);
      }
  
      @Override
      public boolean onUnbind(Intent intent) {
          Log.v(LOG_TAG, "in onUnbind");
          return true;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.v(LOG_TAG, "in onDestroy");
          mChronometer.stop();
      }
  
      public String getTimestamp() {
          long elapsedMillis = SystemClock.elapsedRealtime()
                  - mChronometer.getBase();
          int hours = (int) (elapsedMillis / 3600000);
          int minutes = (int) (elapsedMillis - hours * 3600000) / 60000;
          int seconds = (int) (elapsedMillis - hours * 3600000 - minutes * 60000) / 1000;
          int millis = (int) (elapsedMillis - hours * 3600000 - minutes * 60000 - seconds * 1000);
          return hours + ":" + minutes + ":" + seconds + ":" + millis;
      }
  
      public class MyBinder extends Binder {
          BoundService getService() {
              return BoundService.this;
          }
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FFFFFF"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="oiyioz.com.a13_local_bound_service_4.MainActivity" &gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="-40dp"
          android:src="@drawable/truiton_sq" /&gt;
  
      &lt;Button
          android:id="@+id/print_timestamp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="130dp"
          android:text="Print Timestamp" /&gt;
  
      &lt;TextView
          android:id="@+id/timestamp_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/print_timestamp"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="120dp"
          android:text=""
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
      &lt;Button
          android:id="@+id/stop_service"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/print_timestamp"
          android:layout_centerHorizontal="true"
          android:text="Stop Service" /&gt;
  
      &lt;Button
          android:id="@+id/start_service"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/stop_service"
          android:layout_centerHorizontal="true"
          android:text="Start Service" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>