Android - Notepad example - Why populate onCreate?

I finished the Layout exercise and wonder why they include calling populateFields() in onCreate and onResume .

According to the OnResume Activity Life Cycle , it will always be executed before the action is shown, so why not just?

+8
android
source share
3 answers

I have real production code that fills in the fields and is only called in onResume, and it works fine. I thought one reason is that maybe onResume is called after the activity is shown, but a little googling digs out this (mostly unrelated) stream: http://groups.google.com/group/android- developers / browse_thread / thread / ddea4830bedf8c6c? pli = 1

Quote: onResume () is thus the last thing that happens before the user interface is displayed

That's what Diana Hackborn says, so I think we can trust her :)

+3
source share

In fact, I saw applications (in my application, as well as others) where the fields were filled only in onCreate() , but not in onResume() .

Lets call this app "A".

The effect was that when the user clicked the home button, switched to another application, and then returned to โ€œAโ€, the screen remained black, since โ€œAโ€ was still in memory, and thus the system did not bother call onCreate() , but go directly to onResume() .

So basically I would say (and this time what @Torp wrote) populated the user interface in onResume() and will be executed.

But then this answer is a little off topic, because it does not answer your question "why."

+1
source share

You do not populate onResume because it will be called every time an action is displayed.

Typically, you want to create as few objects as possible, so you will create them once and for all in onCreate, and then you can always verify that they are still being updated in onResume.

0
source share

All Articles