[英]OnDestroy is not being called after the back button is press
I'm trying to put clean up code on my activity. 我正在尝试整理活动代码。 The create function gets called, but the brake poitn i set ondestroy never goes of when the back button is pressed
创建函数被调用,但是当按下后退按钮时,我设置的刹车点永远不会消失
code: 码:
public class cPuzzle extends cBase {
cPuzzleView MyView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
MyView =new cPuzzleView(this, this, cGlobals.PuzleId);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(MyView);
StartTimer(20);
}
void OnDestroy()
{
StopTimer();
MyView.OnDestroy();
}
Your onDestroy
should be defined as 您的
onDestroy
应该定义为
protected void onDestroy() { ... }
and not 并不是
void OnDestroy() { ... }
Java is case-sensitive language. Java是区分大小写的语言。
I'm not surprised. 我不惊讶。
onDestroy()
is not automatically called when you press the back button. 当您按下返回按钮时,不会自动调用
onDestroy()
。 Generally it is only called when the system is low on resources and needs to reclaim some memory. 通常, 仅在系统资源不足且需要回收一些内存时才调用它。 You shoudld be looking at
onPause
or onStop
instead. 您应该查看
onPause
或onStop
。
To make things clearer as to when onDestroy
is called, look at the android lifecycle docs . 为了使
onDestroy
的调用时间更清楚,请查看android生命周期文档 。 An activity is paused if its partially hidden, stopped if it is totally hidden, eg you press the back button. 如果某个活动被部分隐藏,则暂停该活动;如果该活动被完全隐藏,则停止活动,例如,按返回按钮。
onStop
can evolve into a call to onDestroy
but does not necessarily do so. onStop
可以演变为对onDestroy
的调用,但不一定要这样做。
That is because onDestroy
is not necessarily called when the back button is pressed, only when the activity is destroyed by Android and it is still not guaranteed to go into onDestroy
. 这是因为
onDestroy
按下后退按钮时,不一定叫,只有当活动是由Android的破坏,它仍然不能保证进入onDestroy
。 A better option would be to place the clean up code within your onStop()
. 更好的选择是将清理代码放在
onStop()
。
Also, you don't really need the @Override
but you do need super.onDestroy();
此外,你并不真正需要的
@Override
,但你确实需要super.onDestroy();
in order for it to behave as a lifecycle event. 为了使其表现为生命周期事件。
You have this 你有这个
public class cPuzzle extends cBase {
Your class does not extend activity. 您的课程不会扩展活动。
http://developer.android.com/reference/android/app/Activity.html#onDestroy() http://developer.android.com/reference/android/app/Activity.html#onDestroy()
onDestory()
is a activity lifecycle method. onDestory()
是活动生命周期方法。
protected void onDestroy ()
Perform any final cleanup before an activity is destroyed. 在销毁活动之前执行任何最后的清理。
Note: do not count on this method being called as a place for saving data ! 注意: 不要指望此方法被称为保存数据的地方 ! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here.
例如,如果活动正在内容提供商中编辑数据,则这些编辑应在onPause()或onSaveInstanceState(Bundle)中提交,而不是在此处提交。
When the user presses the Back button
, the current activity
is popped from the top of the stack (the activity is destroyed
) and the previous activity resumes
(the previous state of its UI is restored). 当用户按下“
Back button
, current activity
从堆栈顶部弹出(该活动被destroyed
),并且上previous activity resumes
(恢复其UI的先前状态)。
http://developer.android.com/guide/components/tasks-and-back-stack.html http://developer.android.com/guide/components/tasks-and-back-stack.html
If you wish you do clean up do it in onPause
. 如果您希望清理,请在
onPause
进行清理。
After press back your activity is paused, and then stopped. 按下后,您的活动将暂停,然后停止。 Let see on activity lifecycle here: http://developer.android.com/reference/android/app/Activity.html
让我们在这里查看活动生命周期: http : //developer.android.com/reference/android/app/Activity.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.