- 关于是否一定要实现onDestory()方法,官方给出下面的说法
Most apps don't need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy()
意思就是说: 当我们在onCreate方法中执行了一些耗时的操作,例如线程或者其他需要内存开销很大而又不能释放的,就必须在onDestory中释放.
- 关于在onPause()方法中可以执行的一些操作和注意事项
2.1 Stop animations or other ongoing actions that could consume CPU.
停止一些动画或者其他可以释放CPU正在进行的操作
2.2 Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
提交无法保存的变化,但是是只有当用户希望保存在离开之前.
2.3 Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them
释放系统资源,例如广播.或者其他影响到电池生命的资源以及用户不需要的
2.4 Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage within onPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()
简单的意思就是说: 不应该去存储一些用户的信息,除非是自动保存的.并且应该避免一些耗时的CPU操作和读取数据库的操作,因为会造成下一个Acitivty出现延迟
- onStop()方法和onRestart()方法
3.1 In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.
当系统内存不足的时候,系统就会直接杀死你的程序而不通过调用onDestory()方法,所以你应该在onStop()方法中去释放资源
在onResume()方法和onPause()方法中,最好不要做太多的耗时操作,导致UI加载过慢才出来.影响用户体验.
关于onSaveInstanceState()方法: 当用户不覆盖该方法,系统也会自动去保存有设置唯一ID值的控件