gpt4 book ai didi

android - 如何在经历生命周期销毁时关闭 Activity ?

转载 作者:太空狗 更新时间:2023-10-29 14:21:33 29 4
gpt4 key购买 nike

android 关闭/完成 Activity 并仍然希望它通过onPauseonStoponDestroy 的方式是什么?

我知道 Activity.finish(),但是这只能保证调用 onDestroy() 而不是其他两个。原因,根据 android 文档,数据库访问应该在 onStop() 中完成,这样它就不会减慢用户的速度,这是我的 Intent ,将所有“保存”保存到 onStop(),但是,我只是注意到调用 finish() 时不会调用该函数...

那么..这种情况下的解决方案是什么?我的意思是,我可以创建一个 boolean 变量,但是有更复杂的方法吗?还有另一种结束 Activity 的方法,比如调用 onDestory() 是不是做错了什么?

编辑

抱歉,我打错了那部分,我的意思是说在 onStop() 中保存到数据库,而不是在 onDestroy() 中。

Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.

Source

最佳答案

调用 Activity.finish 确实经历了 Activity 生命周期:http://developer.android.com/training/basics/activity-lifecycle/stopping.html

还有 onDestroy() 的文档状态:

Note: do not count on this method being called as a place for saving data!

onPause()状态:

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

最后一件事,onStop() :

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

这让您只需使用 onPause() 作为覆盖和执行一些保存操作的方法。它确实被称为。

在这里找到一个相关问题:finish() and the Activity lifecycle

关于android - 如何在经历生命周期销毁时关闭 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16495976/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com