简体   繁体   English

为什么首选 OnCreate 来执行所有主要的应用程序任务?

[英]Why Is OnCreate Preferred To Do All The Main App Tasks?

Why Is onCreate() Preferred To Do All The Main App Tasks?为什么首选onCreate()来执行所有主要的应用程序任务? Why not onResume() or onStart() ?为什么不是onResume()onStart() Why only onCreate() ?为什么只有onCreate() I tried to do the main tasks like binding findViewById() setting text to text views and a lot more.我尝试完成主要任务,例如将findViewById()设置文本绑定到文本视图等等。 They all work fine.他们都工作正常。 When why do we always are preferred to do that task in onCreate() ?为什么我们总是喜欢在onCreate()中执行该任务?

OnCreate serves as the first entry point into your activity, so logically it makes sense to do as much of the initialization here as possible. OnCreate 是活动的第一个入口点,因此从逻辑上讲,尽可能多地在此处进行初始化是有意义的。 Often times there are cases when things need to get configured with higher priority - crash reporting services, dependency injection etc. where this would get escalated to a custom application class.通常情况下,需要配置更高优先级的东西——崩溃报告服务、依赖注入等,这会升级到自定义应用程序 class。

according to the docs根据文档

protected void onCreate (Bundle savedInstanceState) protected void onCreate (Bundle savedInstanceState)

Called when the activity is starting.活动开始时调用。 This is where most initialization should go : calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI...这是大多数初始化应该 go 的地方:调用 setContentView(int) 来膨胀活动的 UI,使用 findViewById(int) 以编程方式与 UI 中的小部件交互......

so, I suppose it is fair to say that most initialization will get done inside of onCreate, which usually means that if you were to place this into a lifecycle method which could get executed repeatedly, that could be considered redundant as you'd be assigning the same values to variables repeatedly, unless that's something you actually want to do.所以,我想可以公平地说大多数初始化将在 onCreate 内部完成,这通常意味着如果你将它放入一个可以重复执行的生命周期方法中,那可以被认为是多余的,因为你将分配重复将相同的值赋给变量,除非那是您真正想要做的事情。

However, lazy initialization is also a concept to keep in mind, being able to initialize something inside of onCreate doesn't always mean that you should, it is often times better to delay initialization until you actually need the instance.然而,惰性初始化也是一个需要牢记的概念,能够在 onCreate 内部初始化某些东西并不总是意味着你应该,延迟初始化直到你真正需要实例通常会更好。

regarding关于

I tried to do the main tasks like binding findViewById() setting text to text views and a lot more.我尝试完成主要任务,例如将 findViewById() 设置文本绑定到文本视图等等。 They all work fine.他们都工作正常。

they definitely would, findViewById can always be used and isn't limited to being inside of onCreate, in fact the result of findViewById doesn't even have to be assigned to a variable for you to be able to use it他们肯定会,findViewById 总是可以使用,并且不限于在 onCreate 内部,事实上 findViewById 的结果甚至不必分配给变量就可以使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否所有片段都是在FragmentActivity的onCreate()上创建的? - Do the all fragments are created onCreate() of FragmentActivity? 为什么代码在 onCreate() 而不是 onResume() 中工作 - Why do codes work in the onCreate() instead of onResume() 在onCreate上初始化视图的首选方法是什么? - What is the preferred way to initialize views onCreate? onCreate是否在主线程上运行? - Is the onCreate run on the main thread? 为什么当我致电onClick时onCreate崩溃导致Android App崩溃? - Why is onCreate crashing Android App when I call onClick? 为什么在oncreate中使用if条件时我的应用程序崩溃? - Why my app crashes when I use if condition in my oncreate? 为什么应用程序在 setVisibility(View.GONE) 上的 onCreate() 中断? - Why app breaks in onCreate() on line setVisibility(View.GONE)? 为什么我们在OnCreate中检查重叠的片段? (来自文档) - Why do we check for overlapping fragments in OnCreate? (from the documentation) 为什么OnCreate只应在Activity的开头调用一次? - Why do OnCreate should be called only once on the start of Activity? 为什么我们在 onCreate() 而不是在 onStart() 或 onResume() 中设置内容视图? - Why do we set content view in onCreate() and not in onStart() or onResume()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM