Can I track when the user clicks the close app button?

I need to understand when the user closes the application and check the data in the database before that. Is it possible to track when he clicks on the button to close the app?

And is it possible to perform any requests to the database before it closes the application, for example closes Telegram with a swipe?

I tried to track this event but I don’t get it.