How to deal with location services in fragments

0

I am creating an app (Android) with two fragments, one with an address list and another with a map pointing to these addresses. In vertical, they are in tabs sliding and in the horizontal, they are side by side, that is, both are always active.

And both need access to the location. Only at the beginning, but soon at the beginning.

The map uses to centralize the position of the device.

In the list serves as geographic center to limit the suggestions of addresses.

I already have the location in the map pane. I used this lib that works very well and is very simple to use, ie I do not need a guide to find the location.

Just before repeating the formula in the other fragment, I was wondering if there is a better way to do this.

For example, if I knew how to get the location in the activity and move to the fragments, I would not have to repeat it.

Or maybe there is another way. Can you show me?

    
asked by anonymous 01.02.2017 / 05:14

1 answer

0

The solution was to create a class extending Application to use as a singleton .

public class Here extends Application {

 private static Here sInstance;
 public LatLng myLocation;

 public static Here getInstance() {
    return sInstance;
 }

 @Override
 public void onCreate() {
    super.onCreate();
    sInstance = this;
 }

 @Override
 public void onTerminate() {
    // Do your application wise Termination task
    super.onTerminate();
 }

 public LatLng getMyLocation() {
    return myLocation;
 }

 public void setMyLocation(LatLng latLng) {
    myLocation = latLng;
 }
}

When listener of location receives an update it uses Here.setMyLocation() to save it and it becomes available to the application as a whole.

    
08.02.2017 / 02:08