简体   繁体   English

我在 onDestroy() 片段方法中调用了 API,但是当我关闭应用程序时它不会调用

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

How do I call an API inside fragment using kotlin-coroutines before exiting the application?在退出应用程序之前,如何使用 kotlin-coroutines 调用 API 内部片段?

In fact, I want it to be called before leaving the application.事实上,我希望在离开应用程序之前调用它。

Android apps are not like regular desktop apps; Android 应用程序与常规桌面应用程序不同; the Android OS decides when to "close" them by calling onDestroy() . Android 操作系统决定何时通过调用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.一个可能的选择是使用onPause()onStop() ,指示用户何时停止与您的片段交互。 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.但它违反了 Android UX 指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM