在 Android 应用程序中使用生命周期方法
我有一个 Android 应用程序,有四个活动。没有一个很大,我没有线程或服务。
我是否仍然应该实现生命周期方法,例如 onStart()
、onResume()
、onPause()
等?
我尝试在合适的地方插入一些,但似乎有点没有必要。我知道它们是为了为应用程序提供稳定性,但当一个人使用许多线程等时,它似乎更有用。我错了吗?
I have an Android Application that has four activities. None is very large and I have no threads or services.
Should I still implement the lifecycle methods, like onStart()
, onResume()
, onPause()
etc?
I tried to insert some at a suitable place, but it seems a bit unnecessary. I understand they are there to provide stability to the application, but it seems more useful when one is using many threads etc. Am I mistaken?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们通过清理在其他生命周期方法中获取的资源来帮助提高稳定性。
您可能还需要刷新
onResume()
中的数据或在onDestroy()
中保存状态,以便在 Activity 重新启动时可以从上次中断的位置继续。如果您不需要这些功能,则无需实现除
onCreate()
之外的方法。They help with stability by cleaning up resources acquired in other lifecycle methods.
You may also want to refresh data in your
onResume()
or save state inonDestroy()
so that you can pick up where you left off when your Activity restarts.If you don't need these features, then you don't need to implement methods other than
onCreate()
.它们不是为了稳定性,而是为了灵活性。如果您作为开发人员需要在活动生命周期中指定某些行为,则会为您提供可以重写的方法。
因此,就您而言,没有必要覆盖它们。
They are not for stability, but for flexibility. If you as a developer need to specify some behaviour in the moments of activity life you are provided with methods you can override.
So, in your case, it is not necessary to override them.