当我按下我的三星Galaxy s2上的结束按钮时,它不会调用onDestroy方法
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(context, "destroy", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:6)
当活动结束时,没有必要调用它。肯定会被叫onPause
。如果您阅读onDestroy
的{{3}},则会说:
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.
如果您看到此问题documentation,答案Activity OnDestroy never called?会说:
仅当系统资源不足(内存,CPU时间等)时才会调用 onDestroy()
,并决定终止您的活动/应用程序,或者当某人对您的活动调用finish()时。
因此,当您按下该按钮退出应用程序时,不必调用onDestroy
。