Android OnCreate() 与 OnStart()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12468614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnCreate() vs OnStart()
提问by Josh Beckwith
Possible Duplicate:
Difference between onCreate() and onStart()?
Android Activity Life Cycle - What are all these methods for?
可能的重复:
onCreate() 和 onStart() 之间的区别?
Android Activity 生命周期 - 所有这些方法的用途是什么?
What is the difference between OnCreate and OnStart?
OnCreate 和 OnStart 有什么区别?
My understanding is that OnCreate is only called the very first time the application is opened and is never called again. Is this true? Can someone please elaborate in their own words rather than copying and pasting a definition? Thank you!
我的理解是 OnCreate 仅在第一次打开应用程序时调用,并且永远不会再次调用。这是真的?有人可以用他们自己的话详细说明而不是复制和粘贴定义吗?谢谢!
回答by DaveJohnston
As long as your device does not kill the activity, for example due to low system resources, then any time you leave your app and go back, onStart is called. If however the application process is killed, then when you return onCreate will be called again, because all of your resources will have been released.
只要您的设备没有终止活动,例如由于系统资源不足,那么任何时候您离开应用程序并返回时,都会调用 onStart。然而,如果应用程序进程被杀死,那么当您返回时 onCreate 将再次被调用,因为您的所有资源都将被释放。
回答by Eric
The documentation seriously--I'm not kidding--has a very, very pretty imagethat describes how the Activity
lifecycle (how it starts, works, and finishes) operates.
认真的文档——我不是在开玩笑——有一个非常非常漂亮的图像,描述了Activity
生命周期(它如何开始、工作和结束)是如何运作的。
The image, directly linked here, basically says this:
直接链接here的图像基本上是这样说的:
onCreate()
is called when theActivity
is created; that is, it is launched or started. (Additionally, this triggers when the orientation is changed.) It is called again when the process is killed, then returned to.onStart()
is called followingonCreate()
at startup. Additionally, it is also called when the app is navigated back to afteronStop()
(and followingonRestart()
), which occurs after theActivity
is no longer visible (which is during the time that "the user can see the activity on-screen, though it may not be in the foreground and interacting with the user").
onCreate()
在Activity
创建时调用;也就是说,它被启动或启动。(另外,这会在方向改变时触发。)当进程被终止时再次调用它,然后返回。onStart()
onCreate()
在启动时被称为跟随。此外,当应用程序导航回 afteronStop()
(和后续onRestart()
)时也会调用它,这发生在Activity
不再可见之后(在“用户可以在屏幕上看到活动的时间,尽管它可能不可见”)在前台并与用户交互")。
The diagram honestly explains it better than I can in less than 1,000 words.
这张图用不到 1,000 字诚实地解释了它,比我能做到的更好。
回答by David Underwood
This is perhaps best explained by starting with the opposite functions: onDestroy and onStop.
这可能最好从相反的函数开始解释:onDestroy 和 onStop。
onDestroy is called when an activity is disposed of or shut down. The next time it's opened, onCreate is called.
当活动被处理或关闭时调用 onDestroy。下次打开时,将调用 onCreate。
onStop is called whenever an application goes out of view, and is no longer visible. This is usually caused by a new activity being created over the top of the old one. When the activity becomes visible again, onStart is called.
每当应用程序消失并且不再可见时,就会调用 onStop。这通常是由在旧活动之上创建的新活动引起的。当活动再次可见时,将调用 onStart。
The diagram on this page does a really good job of explaining the various states an activity can be in, along with the relevant transition methods: http://developer.android.com/training/basics/activity-lifecycle/starting.html
此页面上的图表很好地解释了 Activity 可能处于的各种状态以及相关的转换方法:http: //developer.android.com/training/basics/activity-lifecycle/starting.html
回答by Joe K
Almost. onCreate is usually called when the app starts, but may also be called if the Activity is destroyed/released for other reasons. Basically, if you want to do one time set-up for the Activity, you should override onCreate. If you want to do something each time the user navigates to the Activity you should override onStart. If you want to do something each time the Activity becomes visible/active, use onResume. If you want to do one-time set up for the whole app, i.e. only runs once EVER, you should use onCreate, but check to see if you've done that set up, and only do it if you have not.
几乎。onCreate 通常在应用程序启动时调用,但也可能在 Activity 因其他原因被销毁/释放时调用。基本上,如果您想为 Activity 做一次设置,您应该覆盖 onCreate。如果您想在每次用户导航到 Activity 时执行某些操作,您应该覆盖 onStart。如果您想在每次 Activity 可见/活动时做某事,请使用 onResume。如果您想对整个应用程序进行一次性设置,即只运行一次,您应该使用 onCreate,但请检查您是否已完成该设置,只有在未完成时才执行。
Check out the very useful diagram on this page: Activity reference
查看此页面上非常有用的图表:活动参考
回答by Hyman Satriano
This website provides a solid graphical representation of the Android lifecycle: http://developer.android.com/training/basics/activity-lifecycle/starting.html
该网站提供了 Android 生命周期的可靠图形表示:http: //developer.android.com/training/basics/activity-lifecycle/starting.html
onStart()
is called whenever the application becomes visible. This includes when the application is first created and when it is brought back on the screen without being terminated. This second behavior occurs when a user switches applications and the app is sitting in the background. When the application becomes invisible, onStop()
is called, but when the application is completely "destroyed", onDestroy()
is called. After an onDestroy()
call, the application must be created again at the beginning of the lifecycle.
onStart()
每当应用程序可见时调用。这包括首次创建应用程序的时间以及在未终止的情况下将其带回屏幕的时间。当用户切换应用程序并且应用程序处于后台时,会发生第二种行为。当应用程序变得不可见时,onStop()
被调用,但当应用程序完全被“破坏”时,onDestroy()
被调用。一个后onDestroy()
调用,应用程序必须再次在生命周期的初期创建的。
It is important to note that onStart()
is not called after the application simply loses focus due to something like a dialog. In such situations, onPause()
is called followed by onResume()
when focus is regained
重要的是要注意,onStart()
在应用程序由于对话框之类的事情而失去焦点后不会调用它。在这种情况下,在重新获得焦点时onPause()
调用onResume()