title: Using LocationListener within a fragment
body: <p>Can I get a users location by using LocationListener and <code>LocationManager</code> from within a fragment? Or do I need to use the <code>LocationListener</code> from within the underlying activity and use an interface (or some other method) to transfer the data back to the fragment? I am in the process of converting an activity to a fragment due to a change app UI. Getting location was no problem when I was using standalone activities, however, I cannot get any location returned now that I have made the activity into a fragment. </p>
  
  <p>Below is how I am adding the LocationListener. It is declared earlier on as LocationListener locationListener.</p>
  
  <pre><code>private void addLocationListener(){
      locationListener = new LocationListener(){
  
          @Override
          public void onLocationChanged(Location location) {
              GeoPoint userLoc = processNewLocation(location);
              if(userLoc != null){
                  Log.d("USERLOC", userLoc.toString());
                                      //do something with the location    
              }
          }
  
          @Override
          public void onProviderDisabled(String provider) {
              locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
              if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
              } else {
                  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
              }
          }
  
          @Override
          public void onProviderEnabled(String provider) {
              locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
              if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
              } else {
                  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
              }
          }
  
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
              // TODO Auto-generated method stub
  
          }
  
      };
  }
  </code></pre>
  
  <p>NOTE: processNewLocation just converts the location to a GeoPoint. It's not even getting that far because no location is ever obtained. </p>
  
  <p>Here's the code registering the listener with the location manager</p>
  
  <pre><code>public void addLocationManager(){
  locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
  }
  
  private void getLocationFromNetwork(){
  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
  Log.d("GET NETWORK", "Listener registered");
  }
  
  private void getLocationFromGPS(){
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
  Log.d("GET GPS", "Listener registered");
  }
  
  private Location getLastLocation(){
  Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  return lastKnownLocation;
  }
  </code></pre>