1

I'm using loader to fetch data from the internet and GoogleApiClient to get the user location i pass a string url with location to the loader constructor ,onCreateLoader method called before onLocationChanged method that get the user location as an object

The problem is i need onLocationChanged method to return the location first to be able to pass a correct url to the onCreateLoader method. So, is there any way to run onLocationChanged method first or delay the loader on create method

onCreateLoader code

    @Override
public android.content.Loader<Adhan> onCreateLoader(int id, Bundle args) {

    Log.v(LOG_TAG, "onCreateLoader invoked query here " + mQueryUrl);
    if (mQueryUrl != null) {
        return new PrayTimeLoader(getContext(), mQueryUrl);
    }
    return null;
}

onLocationChanged code

 @Override
public void onLocationChanged(Location location) {

    mLocation = location;

    makeQueryUrl();


    Log.v(LOG_TAG, "latitude is " + location.getLatitude());
    Log.v(LOG_TAG, "Longitude is " + location.getLongitude());

}

makeQueryUrl code

private String makeQueryUrl() {

    StringBuilder theQuery = new StringBuilder();

    Long tsLong = System.currentTimeMillis() / 1000;
    String ts = tsLong.toString();

    if (mLocation != null) {


        String latitude = Double.toString(mLocation.getLatitude());
        String longitude = Double.toString(mLocation.getLongitude());

        theQuery.append("http://api.aladhan.com/timings/")
                .append(ts)
                .append("?latitude=")
                .append(latitude)
                .append("&longitude=")
                .append(longitude)
        ;

        Log.v(LOG_TAG, "our url for today is " + theQuery);
    }
    mQueryUrl = String.valueOf(theQuery);
    return mQueryUrl;
}

Log output

  • 08-08 20:48:19.406 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: onCreateLoader invoked query here null

    08-08 20:48:19.406 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: onCreateLoader invoked query here null

    08-08 20:48:19.634 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: onConnected invoked true

    08-08 20:48:19.645 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: location is requested

    08-08 20:48:19.652 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: latitude is 30.8567497

    08-08 20:48:19.652 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: Longitude is 30.8001775

MuhammadAliJr
  • 273
  • 4
  • 17
  • Why would you have onCreateLoader be called before you can pass the data it needs (location) ? – android developer Aug 08 '17 at 18:33
  • the same activity implements the **LocationListener** and **LoaderManager.LoaderCallbacks** so both **onCreateLoader** method and **onLocationChanged** is overridden in the same activity , when i used logs figured the onCreateLoader method called before onLocationChanged. – MuhammadAliJr Aug 08 '17 at 18:45
  • 1
    Where exactly do you initialize the loader? The trigger to initialize and start it should be after onLocationChanged . No reason to do it before. – android developer Aug 08 '17 at 19:03
  • indeed you're right thank you :) – MuhammadAliJr Aug 08 '17 at 19:25

2 Answers2

2

why don't you just call the loader from inside of onLocationChanged?

void onLocationChanged(Location location) {
    if (location != null) {
        getLoaderManager().initLoader(0, null, this);
    }
}

alternatively you can run

getLoaderManager().restartLoader(0, null, this);

inside of onLocationChanged()

anomeric
  • 688
  • 5
  • 17
1

It sounds like you are using LoaderManager. I'm not sure where you initialize it, but try this to reinit the loader after the location is captured:

@Override
public void onLocationChanged(Location location) {

    mLocation = location;

    makeQueryUrl();

    Log.v(LOG_TAG, "latitude is " + location.getLatitude());
    Log.v(LOG_TAG, "Longitude is " + location.getLongitude());

    getLoaderManager().restartLoader(0, null, this);

}
amacf
  • 257
  • 2
  • 8