I call an API in the onDestroy() fragment method, but when I close the app it does not call

724 views Asked by At

How do I call an API inside fragment using kotlin-coroutines before exiting the application?

In fact, I want it to be called before leaving the application.

1

There are 1 answers

4
ris8_allo_zen0 On

Android apps are not like regular desktop apps; the Android OS decides when to "close" them by calling onDestroy(). This can happen right after you press the system Back button, or hours later, or never.

A possible option is to use onPause() or onStop(), indicating when the user stops interacting with your fragment. But the user might start interacting with another fragment or activity you may have defined in your app.

Some apps listen to the back button press and ask for confirmation to "exit the app" before doing as much internal cleanup as possible. But it goes against the Android UX guidelines.