Why Is onCreate()
Preferred To Do All The Main App Tasks? Why not onResume()
or onStart()
? Why only onCreate()
? I tried to do the main tasks like binding findViewById()
setting text to text views and a lot more. They all work fine. When why do we always are preferred to do that task in onCreate()
?

- 7,947
- 5
- 29
- 51

- 3,585
- 2
- 7
- 38
-
1I think the calls on that function is more optimized to handle such tasks and it prevents loading the processor with excess work. like if you want to go somewhere, you can always go offroad through a straight path and probably reduce your cars life by half and also experiencing the bumpiest of the rides while also reaching your destination, or you can just use the highway and keep driving the car for years to come. – Vaishnav Kanhirathingal Apr 14 '22 at 06:08
-
1Its the lifecycle of `Activity` ..And nobody forced u to do `findViewById` in `onCreate()` only u can do it anywhere as long as your View is alive in scope . The other methods like `onResume` can be called multiple times when moving back and forth from `Activity` So they serve special purpose.. its all lifecycle and that's how they build.its like why init block gets called as first line of constructor.. – ADM Apr 14 '22 at 06:40
-
1`Why Is onCreate() Preferred To Do All The Main App Tasks? ` i don't think there's any actual documentation which states that onCreate is preferred for all main tasks, do you have any source which says something has to be done from onCreate or is this just based on what you've seen people do ? – a_local_nobody Apr 14 '22 at 09:40
-
@a_local_nobody Most of the youtube tutorials, android docs and android sample app all do these main tasks in the `onCreate()`. This is on what I have seen ppl do – Sambhav Khandelwal Apr 14 '22 at 09:42
-
1that's true, but there are cases where stuff needs to be initialized or done with an even higher priority, which is when the application class comes into play, so i don't think ALL main tasks occur inside the create of an activity, i think it's fair to say that [the majority of initialization](https://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29) gets done inside on create, but again, that can also be an outdated approach, lazy initialization is a thing – a_local_nobody Apr 14 '22 at 09:45
-
1@sambhav-k I was the one who upvoted the question. ;) it was a good curiosity based question. Not something you see often here. Also, the accepted answer is from the docs. so everybody, do read that as it can probably help you improve your apps performance. I was honestly surprised to see this much activity here. – Vaishnav Kanhirathingal Apr 14 '22 at 13:38
1 Answers
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. 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.
according to the docs
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...
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.
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.
regarding
I tried to do the main tasks like binding findViewById() setting text to text views and a lot more. 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

- 7,947
- 5
- 29
- 51