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.
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.
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()
oronStop()
, 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.