onStart()的真正作用是什么? - 安卓 [英] What does onStart() really do? - Android

查看:1155
本文介绍了onStart()的真正作用是什么? - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道onStart()函数在android生命周期中的确切作用.网上的大多数资源只是在说-在您的活动在屏幕上可见之前被调用.

I have been wondering for some time about the exact role of onStart() function in android life cycle. Most of the resources on net just say - it is called just before your activity becomes visible on the screen.

但是到目前为止,我开发的应用程序从未使用过onStart().我在onCreate()本身中进行所有初始化.

But the applications that I have made so far I have never used onStart(). I do all my initialization in onCreate() itself.

android生命周期中的所有其他状态都具有其他意义,但是onStart()的作用不是很清楚.

All other states in android life cycle have some or the other significance but the role of onStart() is not very clear.

在onStart()中是否必须做任何特定的事情?在生命周期中确实需要onStart(),因为所有初始化都可以在onCreate()中完成,并且也可以在活动可见之前调用它.

Are there any specific things that must be done in onStart()? Is onStart() really required in life cycle since all the initialization can be done in onCreate() and it is also called before the activity becomes visible.

任何人都可以帮助我清楚地了解onStart(). 在此先感谢:)

Can anyone help me out to understand onStart() clearly. Thanks in advance :)

推荐答案

onStart().例如,如果您拥有活动A并从中开始活动B,则活动A将被暂停(onPause()),然后停止(onStop()),并移回堆栈.此后,如果将Back按入活动B,则B将被暂停(onPause()),停止(onStop())和销毁(onDestroy()),并且活动A将从后堆栈恢复,开始()并恢复(onResume()).如您所见,系统不会再次为A调用onCreate().

onStart() is called when activity resumes from stopped state. For example, if you have activity A and starts activity B from it, then activity A will be paused (onPause()) and then stopped (onStop()) and moved to back stack. After this, if you press Back into your activity B, B will be paused(onPause()), stopped (onStop()) and destroyed(onDestroy()), and activity A will be restored from back stack, started (onStart()) and resumed(onResume()). As you can see, system will not call onCreate() for A again.

如何使用onStart()?例如,您应该在onStop()中取消注册GPS,传感器等的侦听器,然后在onStart()中再次注册.如果您在onCreate()中进行注册,而在onDestroy()中进行注销,则GPS服务将始终工作,并且将耗尽电池电量.

How to use onStart()? For example, you should unregister listeners for GPS, sensors, etc in onStop() and register again in onStart(). If you register it in onCreate() and unregister in onDestroy(), then GPS service will work always and it will drain battery.

这篇关于onStart()的真正作用是什么? - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆