Reputation: 1991
I want to have a function that is called when my application is closed. All of my activities in my application extend a custom class MenuActivity
which in turn extends the Activity
class. In MenuActivity, I have the following code...
@Override
protected void onDestroy()
{
System.out.println("DESTROY IT");
}
For some reason, I never see this text. I've tried both with and without super.onDestroy();
in there. I want this to be able to be called when the user closes the application (holds the home button to view the open applications and swipe to close), or if the application crashes. I need to do a bit of clean-up on application close (close ongoing notifications, and a few other things).
I thought it should be simple enough to use the onDestroy method, but it never seems to get called. Any ideas?
Upvotes: 2
Views: 2706
Reputation: 3787
method onDestroy()
won't be called every time, but you can do like this:
public void onPause() {
super.onPause()
if (isFinishing()) {
//call some method
}
}
Upvotes: 2
Reputation: 1996
If you want to keep your application running when users are using other applications you need to implement your media player as a foreground service. You can pass a notification when starting this service.
Check out this link: http://developer.android.com/guide/topics/media/mediaplayer.htmlhttp://developer.android.com/guide/topics/media/mediaplayer.html especially the section on "Running as a foreground service"
Upvotes: 1
Reputation: 154
You should use onStop() for this. You can read more about the android activity lifecycle here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
The android developer guides are a great resource.
Upvotes: 0