title: Android onResume method called when activity is not visible
body: <p>I want to the access the gps service in my application.So I used <code>Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);</code> inside the <code>onClick()</code> method of a button.After calling the Intent I start the service to listen for location updates.In the onResume method I check for gps service.If it is disabled,I stop the service.But the onResume method is called when the Location Settings is shown and the service is ultimately stopped even before the user can turn on the gps.</p>
  
  <p>Code:</p>
  
  <pre><code>     Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(i);
       gps_intent=new Intent(AddOffers.this, StatsGpsService.class);
       gps_intent.putExtra("offr_act_ind",true);
       startService(gps_intent);
  
  
  
   @Override
       protected void onResume() {
       super.onResume();
       LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
       boolean isproviderenabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      if(isproviderenabled==false){
          if(gps_intent!=null){
              Log.e("g","g");
          stopService(gps_intent);
      }}
  </code></pre>
  
  <p>StatsGpsService.java:</p>
  
  <pre><code>public class StatsGpsService extends Service {
      LocationListener locationListener;
      LocationManager locationManager;
      boolean flag1;
  
      public StatsGpsService() {
          super();
  
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.e("Service","Created");
  
  
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          handleLocation(intent);
          return super.onStartCommand(intent, flags, startId);
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.e("Service","Destroyed");
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      public void handleLocation(Intent intent){
          if(intent!= null) {
              flag1 = intent.getBooleanExtra("offr_act_ind", false);
  
              locationListener = new LocationListener() {
                  @Override
                  public void onLocationChanged(Location location) {
                      if (flag1 == true) {
  
                          Intent intent = new Intent("loc_updts");
                          intent.putExtra("stat_lat", location.getLatitude());
                          intent.putExtra("stat_longt", location.getLongitude());
                          sendBroadcast(intent);
                      }
                  }
  
                  @Override
                  public void onStatusChanged(String s, int i, Bundle bundle) {
  
                  }
  
                  @Override
                  public void onProviderEnabled(String s) {
  
                  }
  
                  @Override
                  public void onProviderDisabled(String s) {
                      Log.e("Provider","Disabled");
  
                  }
              };
              locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
  
              if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  // TODO: Consider calling
                  //    ActivityCompat#requestPermissions
                  // here to request the missing permissions, and then overriding
                  //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                  //                                          int[] grantResults)
                  // to handle the case where the user grants the permission. See the documentation
                  // for ActivityCompat#requestPermissions for more details.
                  return;
              }
  
              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
          }
      }
  }
  </code></pre>