Jack
Jack

Reputation: 491

How to update fragment from activity

I am making an app with 2 fragments that take up the whole screen and are managed by a viewpager and they both use location data, I get the location data by implementing LocationManager and the use LocationListener to check if the user has moved and needs new data. Both fragments use the users location to display the initial data and both need to know if the user has changed location to display new data. I use the following code to get location data and request updates:

    private void getLocationData() {
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("LOLWeather", "onLocationChange() callback received");
            mLatitude = location.getLatitude();
            mLongitude = location.getLongitude();                
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
            Log.d("LOLWeather", "onProviderDisabled callback received");
        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION} ,REQUEST_CODE);
        return;
    }
    mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
    mLatitude = mLocationManager.getLastKnownLocation(LOCATION_PROVIDER).getLatitude();
    mLongitude = mLocationManager.getLastKnownLocation(LOCATION_PROVIDER).getLongitude();
}

My first question is I figure I can put this code inside each fragment and then onPause within each fragment I could do: if(mLocationManager != null) mLocationManager.removeUpdates(mLocationListener);

However it seems wrong to be requesting location data every time the user swaps from one fragment/view to another and also to be adding and removing the locationListener every time. Since they both use the location data I figured its best to place the getLocationData() method within the activity itself, is this correct??? If not how should I handle it, putting the getLocationData() method in each fragment???

My follow up question unless im wrong, which is where Im stuck, now that I have the getLocationData() method within the main activity when the onLocationChanged() method is called within the locationListener how do I tell the fragment that the location changed so it updates itself??? I originally pass the data to the fragment via a bundle but now that the fragment is created I can make a method within the fragment that updates it once it has the new data, but how do I let it know it has to update itself and give it the new location data???

Thanks.

Upvotes: 0

Views: 1060

Answers (2)

Diego D.
Diego D.

Reputation: 398

Since they both use the location data I figured its best to place the getLocationData() method within the activity itself, is this correct???

I think it is a good way to do it.

how do I tell the fragment that the location changed so it updates itself???

You can send a broadcast when you catch your location update, and register a receiver to this event to update your fragment.

e.g:

When you catch the update event in your activity's method:

sendBroadcast(new Intent("action_location_updated"));

And then, in your fragment:

BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("action_location_updated".equals(intent.getAction())) {
                // update fragment here
            }
        }
    };
IntentFilter filter = new IntentFilter("action_location_updated");
registerReceiver(mReceiver, filter);

NOTE: It's better to use the action's string as a static final variable:

public static final ACTION_UPDATE_FRAGMENT = "action_location_updated";

Upvotes: 1

user6575276
user6575276

Reputation:

The Right Way In my opinion Is to put the function getLocationData() in Activity and from there send a Broadcast with the new position.

this is link with explain on broadcat.

https://developer.android.com/guide/components/broadcasts.html

Upvotes: 1

Related Questions