title: Service Automatic Called on Destroying Activity
body: <p>I am stuck with the problem of <strong><code>Activity + Service</code></strong> in that I have following number of Activities and Services.</p>
  
  <h3>Activities:</h3>
  
  <blockquote>
    <p>LoginActivity => OrderListActivity => AddOrderActivity => ConfirmOrderActivity</p>
  </blockquote>
  
  <h3>Services:</h3>
  
  <ol>
  <li><strong>ReceivingOrderService</strong> - Receiving New Data From Server</li>
  <li><strong>SendingOrderService</strong> - Sending new Data to Server</li>
  </ol>
  
  <p>Above both Service Calling from another Separate Service on duration of some <strong>interval</strong>.</p>
  
  <ol start="3">
  <li><strong>CheckAutoSyncReceivingOrder</strong> - To call ReceivingOrderService (Interval 15Mins)</li>
  <li><strong>CheckAutoSyncSendingOrder</strong> - To call SendingOrderService (Interval 3Mins)</li>
  </ol>
  
  <h3>CheckAutoSyncReceivingOrder:</h3>
  
  <pre><code>public class CheckAutoSyncReceivingOrder extends Service {
  
      Timer timer;
  
      @Override
      public IBinder onBind(Intent arg0) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          // TODO Auto-generated method stub
  
          if(timer != null) {
              timer.cancel();
              Log.i(TAG, "RECEIVING OLD TIMER CANCELLED&gt;&gt;&gt;");
          }
  
          timer = new Timer();
  
          timer.schedule(new TimerTask() {
              @Override
              public void run() {
                  if(InternetConnection.checkConnection(getApplicationContext())) {
                      if(getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
                          startService(new Intent(CheckAutoSyncReceivingOrder.this, ReceivingOrderService.class));
                  } else {
                      Log.d(TAG, "Connection not available");
                  }
              }
          }, 0, 60000); // 1000*60*15 = 9,00,000 = 15 minutes
      }
  
      @Override
      public void onDestroy() {
          // TODO Auto-generated method stub
          super.onDestroy();
  
          if(timer != null)
              timer.cancel();
  
          Log.d(TAG, "Stopping Receiving...");
      }
  }
  </code></pre>
  
  <h3>CheckAutoSyncSendingOrder:</h3>
  
  <pre><code>public class CheckAutoSyncSendingOrder extends Service {
  
      Timer timer;
  
      @Override
      public IBinder onBind(Intent arg0) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          // TODO Auto-generated method stub
  
          if(timer != null) {
              timer.cancel();
              Log.i(TAG, "OLD TIMER CANCELLED&gt;&gt;&gt;");
          }
  
          timer = new Timer();
  
          timer.schedule(new TimerTask() {
              @Override
              public void run() {
                  Log.i(TAG, "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; SENDING AUTO SYNC SERVICE &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;");
                  if(InternetConnection.checkConnection(getApplicationContext())) {
                      if(getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
                          startService(new Intent(CheckAutoSyncSendingOrder.this, SendingOrderService.class));
                  } else {
                      Log.d(TAG, "connection not available");
                  }
              }
          }, 0, 120000); //  1000*120*15 = 1,800,000 = 15 minutes
      }
  
      @Override
      public void onDestroy() {
          // TODO Auto-generated method stub
          super.onDestroy();
  
          if(timer != null)
              timer.cancel();
  
          Log.d(TAG, "Stopping Sending...");
      }
  }
  </code></pre>
  
  <h3>ConfirmOrderActivity#Final Task which i have called for Insert Data:</h3>
  
  <pre><code>new AsyncTask&lt;Void, Void, Integer&gt;() {
  
      ProgressDialog progressDialog;
  
      @Override
      protected void onPreExecute() {
          // TODO Auto-generated method stub
          super.onPreExecute();
  
          progressDialog = new ProgressDialog(
                  ConfirmOrderProductActivity.this);
          progressDialog.setMessage("Inserting "
                  + (isInquiry ? "Inquiry" : "Order") + "...");
          progressDialog.setCancelable(false);
          progressDialog
                  .setProgressStyle(ProgressDialog.STYLE_SPINNER);
          progressDialog.show();
      }
  
      @Override
      protected Integer doInBackground(Void... params) {
          // TODO Auto-generated method stub
          int account_id = context.getSharedPreferences(PREF_DATA,
                  MODE_APPEND).getInt(DATA_ACCOUNT_ID, 0);
  
          /**
           * Check Whether isInquiry or not...
           */
          product_type = isWeight ? 1 : 0;
          if (isInquiry) {
              /*
               * INSERTING DATA IN INQUIRY TABLE
               */
              return m_inquiry_id;
          } else {
              /*
               * INSERTING DATA IN ORDER TABLE
               */
              return m_order_id;
          }
      }
  
      @Override
      protected void onPostExecute(Integer m_order_id) {
          // TODO Auto-generated method stub
          super.onPostExecute(m_order_id);
  
          progressDialog.dismiss();
  
          if (dbHelper.db.isOpen())
              dbHelper.close();
  
          String title = "Retry";
          String message = "There is some problem, Go Back and Try Again";
  
          AlertDialog.Builder alert = new AlertDialog.Builder(
                  ConfirmOrderProductActivity.this);
  
          if (m_order_id != -1) {
              title = isInquiry ? "New Inquiry" : "New Order";
              message = isInquiry ? "Your Inquiry Send Successfully." : "Your Order Saved Successfully.";
              alert.setIcon(R.drawable.success).setCancelable(false);
          } else {
              alert.setIcon(R.drawable.fail).setCancelable(false);
          }
  
          alert.setTitle(title).setMessage(message)
                  .setPositiveButton("OK", new OnClickListener() {
  
                      @Override
                      public void onClick(DialogInterface dialog,
                              int which) {
                          // TODO Auto-generated method stub
                          dialog.dismiss();
                          startActivity(new Intent(
                                  ConfirmOrderProductActivity.this,
                                  FragmentChangeActivity.class)
                                  .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
  
                          /* Opening Left to Right Animation */
                          overridePendingTransition(R.anim.right_out,
                                  R.anim.right_in);
                      }
                  });
  
          AlertDialog alertDialog = alert.create();
          alertDialog.show();
  
      }
  }.execute();
  </code></pre>
  
  <p><strong>Everything is working fine as per flow of inserting records in database.</strong></p>
  
  <h1>After Adding Inquiry:</h1>
  
  <p><a href="https://i.stack.imgur.com/IYXCj.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/IYXCj.jpg" alt="enter image description here"></a></p>
  
  <h1>Destroying Activity and Getting following Logcat:</h1>
  
  <p><a href="https://i.stack.imgur.com/EpiF4.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/EpiF4.jpg" alt="enter image description here"></a></p>
  
  <h2><strong>Main Problem:</strong></h2>
  
  <p>When I placed  order successfully from <strong>ConfirmOrderActivity</strong>, It is displaying <code>AlertDialog</code> of <strong>Success</strong> Message which is <strong>cancellable</strong> <code>false</code>. When I Stop application from this Activity, <strong>Its calling both <code>CheckAutoSyncReceivingOrder</code> and <code>CheckAutoSyncSendingOrder</code> automatically.</strong></p>
  
  <h3>Edited:</h3>
  
  <blockquote>
    <p>I am calling both Service from <code>LoginActivity</code> only, after that it
    will called automatically after given intervals But Problem occurs
    when I destroy <strong>ConfirmOrderActivity</strong> when dialog is shown.</p>
  </blockquote>
  
  <p>I didn't know why it happens that Why its running automatically when I stop Activity Directly.</p>
  
  <h2>I have tried <code>onStartCommand()</code> with <code>START_NON_STICKY</code> in <code>Service</code> but not working. (as <strong><code>START_STICKY</code></strong> is default.)</h2>
  
  <pre><code>@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      return START_NOT_STICKY;
  }
  </code></pre>
  
  <p>Is there any solution?</p>