Search code examples
androidserviceondestroy

How to call a function when a user swipes off the application in android?


I have an application that uses PortSip SDK to make VoIP phone calls, and i need to close the phone call when the users swipes the app off but i want to leave the call when the app is in background. i have a function endCall() which ends the call but if i call it on onDestroy() it gets called even if the app is in background.


Solution

  • I haven't really looked into the PortSip SDK but it sounds more like an issue of understanding the lifecycle of an activity. If the app goes to the background, the onDestroy() will be called by the system if needed. Here is the official documentation: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle.

    What you want to do is achieved with a Service which will be running in the background and doesn't get killed by the system if the app isn't open. Have a look at: http://developer.android.com/guide/components/services.html.