开发者

Android - Notepad example - Why populate in onCreate?

I have finished the Layout exercise and wondering why th开发者_JAVA技巧ey include the call to populateFields() in both onCreate and onResume.

According to Activity Lifecycle "onResume" will always be performed before the Activity is shown so why not just there?


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

Quote: onResume() is thus the last thing that happens before the UI is shown

This is what Dianne Hackborn says so i guess we can trust her :)


Actually I have seen apps (in my app and also others), where fields were only populated in onCreate(), but not in onResume().

Lets call that app 'A'.

The effect was that when the user pressed the home button, went to a different app, and then returned to 'A', the screen stayed black, as 'A' was still in memory and thus the system did not bother to call onCreate(), but directly went into onResume().

So basically I'd say (and this seconds what @Torp wrote) populate the UI in onResume() and be done.

But then, this answer is slightly off-topic as it does not answer your "why" question.


You don't populate in onResume because it will be called every time the activity is shown.

You generally want to create as few objects as possible, so you create them once and for all in onCreate, and then you can always check that they are still updated in onResume.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜