Search code examples
androidlocationmanagerlocationlistener

Why location manager is still on when I make it null?


In my app, I am using two location managers for two different activities. First location manager is created when the first activity is created. And then from the first activity I am creating a second activity which is creating a second location manager. In the second activity, I am trying to stop location manager updates using the following code when the back button is pressed:

@Override
public void onBackPressed() {
  lm.removeUpdates(ll);
  ll = null;
  lm = null;
  finish();
}

ll and lm are declared globally in both first and second activity seperatly. ll and lm are initialized in onCreate of second activity using following code:

 lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 ll = new myLocationListener();
 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);

The problem is that if I don't go to the second activity and stop the first activity's location updates, then the GPS sign in my Android goes away. But if I go to the second activity, come back to the first activity using the back button and then stop the first acivity's location updates, then the GPS location sign is still there. I am stopping updates in the first activity using same line of codes:

lm.removeUpdates(ll);
ll = null;
lm = null;

ll and lm in first activity are initialized when start button is pressed:

 public void startClick (View target){
      lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      ll = new myLocationListener();
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
 }

Is there any kind of mistake in the code or is there any logical error? Thanks in advance.


Solution

  • You can have a single instance of the LocationManager and store it in your Application Context. To do so you need to create a class that extends Application. Here's an example:

    public class MyApplication extends Application 
    {
       private LocationManager locationManager;
    
       @Override
       public void onCreate()
       {
         super.onCreate();
    
         //Initialize here the locationManager or initialize it in one of your
         //Activities. It is your choice.
       }
    
       //To retrieve the locationManager
       public LocationManager getLocationManger()
       {
          return locationManager;
       }
    
       //In case you want to initialize the locationManger in one of your Activities
       public void setLocationManager(LocationManager locationMangerIn) 
       {
           locationManager = locationMangerIn;
       }
    }
    

    Do not forget to add a reference to MyApplication in the android:name attribute in the AndroidManifest.xml file. Something like this:

    <application android:icon="@drawable/icon" android:label="@string/app_name"
            android:name="com.your_package_name.MyApplication">
    

    To access your locationManager instance you call this piece of code:

    LocationManger lm = ((MyApplication)activitysContext.getApplication()).getLocationManger();
    

    To instantiate the locationManager instance inside your Activity you use this:

    public void startClick (View target){
      LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
      //You can add the listener now or later. Again, your choice.
      ll = new myLocationListener();
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
    
      ((MyApplication)activitysContext.getApplication()).setLocationManger(lm);
    

    }

    Hope that helps:)