I'm trying to call a function when user closes app manually (force close/manually closing through recent apps etc.) or when the OS closes the app to free up memory. I'm doing database calls in my function.
My app architecture is based on fragments for each screen (one MainActivity with FragmentContainerView), navigation is using JetPack navigation.
I have tried calling my function in onDestroy(), onDestroyView(), onDetach() - none of them are called when manually closing the app. onStop() fires prematurely (when fragment isn't in view anymore), but I need it when the app closes.
I'm using kotlin.
How do go about doing this?
override fun onDestroy() {
CoroutineScope(Dispatchers.IO).launch{
//Database calls
}
super.onDestroy()
}