开发者

Android Notepadv3 tutorial - Is saveState() in onSaveInstanceState really necessary?

I am only a beginner to Android, but I have noticed a number of things that seem a little strange in the third notepad tutorial:

  • The tutorial explicitly states that you need to call saveState from BOTH onSaveInstanceState and onPause. Is this really nece开发者_开发技巧ssary? It seems to me from reading the process life-cycle documentation that onPause will always be called before the Activity is killed, regardless of whether it is done so by the system or the user. If this is the case, surely just calling saveState from onPause is sufficient?
  • Calling populateFields() in both onResume and onCreate is pointless, since onResume is always called after onCreate anyway. Hence if I understand correctly, the call in onCreate serves no useful purpose and can be safely removed.

Could someone please either confirm these points, or let me know what I have misunderstood.

Olly


From what I understand...

You are correct that onResume is always called eventually after onCreate, and you can often put the code there. Keep in mind though that onResume will also be called when an activity comes back to the foreground, without first going through onCreate. So for one time initialization code (like setContentView() or initializing variables) the onCreate event is a better place.

Also, you are correct about the onPause and onStop. onPause is where you'd normally code the state handling. onPause is killable so that onStop can potentially not even get called in very low memory situations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜