开发者

call method when program exits - onDestroy not reliable

I want to execute some functions when the program is exited by hitting the back button. This is now done by onDestroy() which works in every case but one. When coming back from another activity in some cases on exiting the program, onDestroy is not called.

I know that in theory onDestroy should only be called when Android closes the app due to low memory, but for me, onDestroy works always and only in a very special case it does not.

Using onPause or onStop does not work because I only want to call the function when the program is exited but not when just another activity is called.

So is the last way to catch the back-button-click an开发者_Go百科d call the function there? Or is there any other solution?


Tactically, use onBackPressed().

Strategically, reconsider your architecture. A well-written activity should not care if onDestroy() is called, as it is guaranteed to NOT always be called. For example, Android can terminate your process whenever it wants (e.g., extreme low memory conditions). The fact that you need onDestroy() to work reliably suggests there are problems that should be resolved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜