android 关闭/完成活动并仍希望其完成的方式是什么onPause
, onStop
and onDestroy
?
我知道Activity.finish()
,但这只能保证调用onDestroy()
不是另外两个。原因,根据android文档,数据库访问应该在onStop()
所以它不会减慢用户的速度,这就是我的意图,将所有“保存”保留到onStop()
,但是,我只是注意到该函数在以下情况下不会被调用finish()
叫做...
那么..这种情况下的解决方案是什么?我的意思是,我可以创建一个boolean
变量,但是有没有更复杂的方法?甚至还有另一种结束活动的方式,比如调用onDestory()
这是不正确的做法吗?
EDIT
抱歉,我打错了那部分,我的意思是保存到数据库中onStop()
,不在onDestroy()
.
虽然 onPause() 方法是在 onStop() 之前调用的,但您应该
使用 onStop() 执行更大、CPU 密集度更高的关闭
操作,例如将信息写入数据库。
Source http://developer.android.com/training/basics/activity-lifecycle/stopping.html
调用 Activity.finish 确实会经历活动生命周期: http://developer.android.com/training/basics/activity-lifecycle/stopping.html http://developer.android.com/training/basics/activity-lifecycle/stopping.html
还有文档onDestroy() http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29状态:
注意:不要指望调用此方法作为保存数据的地方!
The onPause() http://developer.android.com/reference/android/app/Activity.html#onPause%28%29 states:
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() http://developer.android.com/reference/android/app/Activity.html#onStop%28%29:
请注意,在内存不足的情况下,系统可能永远不会调用此方法,即系统没有足够的内存来在调用其 onPause() 方法后保持活动进程的运行。
这让你只剩下onPause()作为覆盖并执行一些保存操作的方法。它确实被称为。
在这里发现了一个相关问题:finish() 和 Activity 生命周期 https://stackoverflow.com/questions/12655898/finish-and-the-activity-lifecycle
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)