0

So I've looked up similar problems to this, and have followed the advice in those threads, but it seems to give me no change in behavior.

So I'm writing an app that essentially notifies the user when they're going too fast or too slow based on GPS onLocationChanged() calls. I overrided the onBackPressed() method to finish() and return but the activity continues to run in the background when I go back to the main activity.

To be clear, I DO want the activity to run when the app is minimized or screen is off. I only want it to stop when the user goes back to the menu (IE, hits the back button)

Here's the link to the class on pastebin: http://pastebin.com/V7z5c3HH

Thanks for your help! =D

Dale Diaz
  • 11
  • 3
  • Check [Android destroying activities, killing processes](http://stackoverflow.com/questions/14375720/android-destroying-activities-killing-processes) you must first know android lifecycle to understand the answer – Jordi Castilla Apr 16 '15 at 16:08
  • Check Android Activity Life Cycle on developer documentation and also research about Services, if you want to stop your location service then you override the onDestroy method of the Activity Life Cycle before calling super.onDestroy(); just unsubscribe the location service (stop the location service.) @Dale Diaz Here is the link for understanding life cycle https://developer.android.com/guide/components/activities/activity-lifecycle and also service life cycle https://developer.android.com/guide/components/services – Asad Mukhtar Mar 25 '19 at 19:45

2 Answers2

0

Unsubscribe your location listener in the onDestroy method. However, what you need for your GPS processing is probably a Service, not an Activity.

clemp6r
  • 3,665
  • 2
  • 26
  • 31
  • So how would I do that? Should I override the onDestroy method? I've never really done this before so forgive my ignorance. Also what is the distinction between a service and an activity? I initially tried getting this to run as a service, but I couldn't seem to get the location information out of it, so displaying the information was difficult. Looking at it now, I assume I should just have setValue methods in my activity, which runs the service, so the service can just call those methods to update the values? I suppose – Dale Diaz Apr 16 '15 at 16:14
  • 1
    a service runs in the background and an activity runs in the foreground(a display screen). re-search more about it @DaleDiaz – Elltz Apr 16 '15 at 16:17
0

You need to remove the updates for that listener.

@Override
protected void onDestroy() {
    locationManager.removeUpdates(locationListener);
    super.onDestroy();
}
ac0de
  • 260
  • 2
  • 10