开发者

Android - Notepad tutorial - lifecycle - some work done twice?

开发者 https://www.devze.com 2023-01-16 10:47 出处:网络
According to the \"Application Fundamentals\" article, section \"component lifecycle\", onResume() is always called when a View becomes active, independent of the previous state.

According to the "Application Fundamentals" article, section "component lifecycle", onResume() is always called when a View becomes active, independent of the previous state.

In the Notepad tutorial, Exercise 3, I have found something confusing in NoteEdit.java:

There is a call to populateFields(开发者_如何学Python) in onCreate() as well as in onResume().

Wouldn't it be enough (or even better) to have it only in onResume() ?

In such a small example, it will not do any harm if populateFields() is performed twice, but in a bigger App, things can be different ...

Thanks and Regards,

Markus N.


From a look at Notepad3, I would say you are correct. There doesn't seem to be any reason for them to call populateFields() in both onCreate() and onResume(). onResume is sufficient.


I can see where you need it in both places, if application pauses then you would need it in onResume and if your process gets killed or user navigates back to activity then you will need it in onCreate especially if you are doing some pre-processing.

Per the documentation....for onResume() they recommend using it for lightweight calls unlike in onCreate():

"The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight. "

The Notepad app may want a variable declared if the method was already hit by onCreate not to redo in onResume().

0

精彩评论

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