问题描述
什么是闭合/ Android的方式完成的活动,仍然希望它去通过的onPause
,的onStop
和的onDestroy
?
What is the android way of closing/finishing an activity and still wanting it to go through onPause
, onStop
and onDestroy
?
我知道的Activity.finish()
,但是这将只能保证调用的onDestroy()
不是另外两个。究其原因,基于Android的文件上,数据库访问,应在的onStop()来完成
所以也没有用户,这是我的意图,让所有放慢节省对的onStop()
,但是,我刚才注意到,当完成()
被称为该函数不叫...
I am aware of Activity.finish()
, however that will only guarantee calling onDestroy()
not the other two. Reason, based on the android documentation, database access should be done in onStop()
so it doesn't slow down the user, which is my intent, to keep all "saves" to the onStop()
, however, I just noticed that that function is not called when finish()
is called...
所以..什么是在这种情况下,如何解决?我的意思是,我可以创建一个布尔
变量,但还有一个更复杂的方式?即使是另一种方式来结束活动,如调用 onDestory()
是不正确的东西怎么办?
So.. what is the solution in this case? I mean, I can create a boolean
variable, but is there is a more sophisticated way? Even another way to end an activity, like invoking onDestory()
is that something incorrect to do?
修改
对不起,我misstyped那部分,我的意思是在的onStop说保存到数据库中()
,而不是的onDestroy()
。
Sorry I misstyped that part, I meant to say save to database in onStop()
, not in onDestroy()
.
虽然的onPause()方法的onStop()调用之前,你应该
使用的onStop()来执行更大,更密集的CPU关机
操作,如向数据库写入信息。
推荐答案
调用Activity.finish并通过在活动的生命周期:
Calling Activity.finish does go through the activity life cycle: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
也为状态:
注:不依靠此方法被称为作为一个地方保存数据
的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.
和最后一件事,对于:
And one last thing, for onStop():
请注意,此方法可能永远不会被调用,在内存不足的情况下,系统没有足够的内存,让您的活动的进程中运行它的onPause()方法被调用后。
这让你只需用的onPause()的覆盖,并执行一些东西保存方法。它的确是被调用。
Which leaves you just with onPause() as the method to override and perform some saving stuff. And it is indeed called.
在发现了一个相关的问题在这里:
In found a related question here: finish() and the Activity lifecycle
这篇关于如何经历的生命周期,而破坏关闭一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!