LocationUpdateListener is not called after activity resume

Hello,

I have an activity with a TomtomMap integrated as a MapFragment (as explained in the getting started documentation). I also registered a LocationUpdateListener to be able to track a chevron. The basic scenario is working fine. LocationUpdateListener.onLocationChanged() is called as expected.

Now when I switch off the screen (by pushing the device’s power button) ans switch it on again the LocationUpdateListener.onLocationChanged() is not called any more. I even tried to unregister my LocationUpdateListener instance in activity’s onPause() and register it again in onResume() but this didn’t help.

It seems that the activity life cycle is not handled correctly or do I overlook something?

Any help is appreciated!

Best regards,
Leo

I did a couple of tests using two example applications: Maps SDK Example App and Time To Leave App.

In both apps, I cannot reproduce the issue. I’m getting location updates each and every time also after switching off the screen or switching back from the different apps or home screen.

Please check and test the above apps and if the issue will still exist, share more details (phone model, Android version, etc) alongside relevant source code.

1 Like

Hello Mateusz,

I could reproduce my issue using the Maps SDK Example App I cloned today. I started the app in debug mode and set a breakpoint in the SearchFragmentPresenter.onLocationChanged() method. Than I went to “Address Search” menu in the app and just brought the corresponding fragent to view without actually searching any address. My breakpoint hit almost every second as expected. Than I switched the display off (by pushing the device’s power button) and turned it back on after a couple of seconds. My breakpoint is never hit gain.

I’m running the app on a Samsung Galaxy Active Tab2 running Android 9.

He is the app’s view I used to reproduce the issue:

Hello.
Thanks a lot for your effort.

Indeed I can reproduce the issue now. It seems that the problem is lying in the fact, that locationSource.deactivate(); removes all location update listeners which are not added back in locationSource.activate();.
As a workaround, I propose to add your location listener back right after the activate(); call just like this:
locationSource.activate();
locationSource.addLocationUpdateListener(your_location_update_listener);
After that, you should be able to receive your location updates again.

In the meantime, we’ll fix the example application and provide either a fix for the LocationSource class or the proper update inside the documentation.

Let us know if the proposed solution works for you.

1 Like

Hello Mateausz,

I tried what you suggested as a workaround but it didn’t solve the issue for me. After switching the screen back on I still do not get any location updates.

In onSuspend() I do:

...
if(tomtomMap != null)  {
      if(this.isTrackingRunning) stopTracking(true);
      tomtomMap.removeLocationUpdateListener(matcherLocationUpdateListener);
      tomtomMap.getLocationSource().deactivate();
}
super.onPause();
...

and in onResume():

...
super.onResume();
if(this.tomtomMap != null && this.matcherLocationUpdateListener != null) {
  LocationSource locationSource = tomtomMap.getLocationSource();
  locationSource.activate();
  locationSource.addLocationUpdateListener(matcherLocationUpdateListener);
}
if(this.isTrackingRunning) startTracking();
...

Best regards,
Leo

Thanks a lot for your effort.
It seems that additionally to the original issue, there is also a problem with a map.getLocationSource() method which for some reason works after the second onResume call.
I’ve created a gist in which location updates works every time. I hope that the following workaround will work for you until we’ll solve it: https://gist.github.com/szczepanczyk/8a17e2eb6eccc9d11537a05074037b9b

Let us know if this helps.

Regards,
Mateusz

Hello Mateusz,

your last workaround for the workaround (from gist) worked for me, thanks! But it definitely should be a workaround, not least because the use of the LocationSourceFactory class is marked as deprecated.

By the way, in your gist in line 54 you add the location update listener directly to the map. It seems that this adding right before the creation of the location source in line 55 implicitly activates the subsequently created location source. In my opinion it would be more straight forward to activate the location source right after it was created in line 55 instead. In my tests it worked fine.

Best regards,
Leo

1 Like

Hi Leo,

Indeed the LocationSourceFactory is deprecated, from now on we encourage people to create LocationSources directly through BasicLocationSource or FusedLocationSource. I will add the missing note in Javadocs.

To obtain an LocationSource create one of this instances in onCreate() like this:

LocationSource locationSource = new BasicLocationSource(context)
LocationSource locationSource = new FusedLocationSource(context, locationRequest)

That way you need to directly add the listener and activate the source in onResume() method using:

locationSource.addLocationUpdateListener(listener)
locationSource.activate()

Regards,
Damian

1 Like

Actually what’s happening here is after you call:

this.map.setMyLocationEnabled(true);

The LocationSource inside of TomTomMap is created and activated, then after you pass an listener in line 54 it’s being called from the TomTomMap, after first onPause() -> onResume() the callback is removed from the TomTomMap and the actual LocationSource you created in line 55 will be activated

1 Like