How to deal with Location services in fragments

Asked

Viewed 58 times

0

I’m creating an app (Android) with two fragments, one with a list of addresses and the other with a map pointing to these addresses. Vertically, they are sliding tab and horizontal, they are side by side, IE, both are always active.

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

The map uses to open centered on the position of the appliance.

In the listing serves as geographic center to limit address suggestions.

I already get the location in the map panel. I used this lib that works very well and is very simple to use, IE, I do not need guide to find the location.

Only before I repeat the formula on the other fragment, I wanted to know if there’s a better way to do it.

For example, if I knew how to pick up the location on Activity and move on to the fragments, then I wouldn’t need to repeat.

Or maybe there’s another way. You can show me?

  • Look at this reply

  • I asked myself that question. I understood how to pass information from one fragment to another and to mainActivity. At least when the processing is direct (synchronous). But not in the case of call backs. And this is the case. Why the Fusedlocation method returns the location asynchronously. Could you give me a hint in this direction or point out some article/answer?

  • Having no more information, in principle, just call the fragment method in the method(callback) of the Activity that receives the location.

  • Rene, it’s been made clear?

  • 1

    I think I got it. But let me just make sure.

  • I’m thinking about how to do that. I imagine I have to do a couple of methods on each fragment that would be getter and Setter of a Latlng field (representing the location). So, on mainActivity I get the location and call the setters methods of the fragments. Then, inside the fragments, I would use the getters to get the location inside them. That’s right, this reasoning?

  • Maybe we don’t need the getters. As the result is asynchronous, Setter, in addition to assigning the value to the field, may need to update something else, possibly some view (I don’t know what Ragment does).

  • In one of the fragments, the location serves to center a googlemap. On the other, it serves as the center for a field LatLngBounds which in turn is used to delimit suggestions in a AutoCompleteTextView Anyway, I’m using your tips to evolve the app.

Show 3 more comments

1 answer

0


The solution was to create a class extending Application in order 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 the listener location receives an update uses Here.setMyLocation() to keep it and it becomes available to the app as a whole.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.