First of all, you need to differentiate between quitting, and pausing the application(a pause happens if another application comes to the foreground). Secondly, you need to override the appropriate methods to handle what happens when the application is paused or destroyed.
For example, overriding
protected void onPause() {}
will allow you to define what should happen when the application is paused, hence, you can gracefully stop whatever your application is doing.
Similarly, you can implement onStop
and onDestroy
if needed. But in your case, I believe onStop
and onPause
will be sufficient :)
Also, try giving this page a look, it gives a detailed description of the activity lifecycle http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle