Android生命周期:在onStart()或onResume()中填写activity中的数据?
是否应该通过光标获取数据并在onStart()
或onResume()
中填写屏幕上的数据,例如设置窗口标题?< /em>
onStart()
似乎是合乎逻辑的地方,因为在 onStart()
之后,Activity 已经可以显示,尽管是在后台。值得注意的是,我在托管对话框方面遇到了问题,这让我重新思考了这一点。如果用户在对话框仍打开时旋转屏幕,则 onCreateDialog()
和 onPrepareDialog()
会在 onStart() 之间调用
和 onResume()
。如果对话框需要基于数据,则需要在 onResume()
之前获取数据。
如果我对 onStart()
的看法是正确的,那么为什么 Notepad 示例 在 onResume()
中执行此操作会给出一个不好的示例?请参阅 http://developer.android。 com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html NoteEditor.java 第 176 行(title = mCursor.getString...
)。
另外,如果我的活动启动另一个活动/对话框来更改我的光标正在跟踪的数据,该怎么办?即使在最简单的情况下,这是否意味着我必须手动更新我以前的屏幕(主活动中对话框的侦听器),或者我必须注册一个 ContentObserver,因为我'我不再更新 onResume() 中的数据(当然我可以更新两次)?
我知道这是一个基本问题,但令我惊讶的是,直到最近的对话才让我意识到这一点。
Should you get data via a cursor and fill in the data on the screen, such as setting the window title, in onStart()
or onResume()
?
onStart()
would seem the logical place because after onStart()
the Activity can already be displayed, albeit in the background. Notably I was having a problem with a managed dialog that made me rethink this. If the user rotates the screen while the dialog is still open, onCreateDialog()
and onPrepareDialog()
are called between onStart()
and onResume()
. If the dialog needs to be based on the data you need to have the data before onResume()
.
If I'm correct about onStart()
then why does the Notepad example give a bad example by doing it in onResume()
? See http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html NoteEditor.java line 176 (title = mCursor.getString...
).
Also, what if my Activity launches another Actvity/Dialog that changes the data my cursor is tracking. Even in the simplest case, does that mean that I have to manually update my previous screen (a listener for a dialog in the main activity), or alternatively that I have to register a ContentObserver, since I'm no longer updating the data in onResume() (though I could update it twice of course)?
I know it's a basic question but the dialog only recently, to my surprise, made me realize this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
同样,解决方案取决于适合您的方案。
如果您希望每个应用程序预填充一次光标(并且不关心任何更改,那么您可以在 onCreate() 中执行此操作。仅当应用程序进程被终止并重新启动应用程序时,才会调用此方法。
如果您希望每次可见生命周期开始时都预填充光标(大多数情况下服务/广播正在调用您的活动,您应该使用 onStart()
如果您希望为活动的每个前台生命周期预填充光标,您应该使用 onResume()因此,如果您有一个对话框或另一个子活动修改了某些信息,因此您想要重新加载光标,最好在 onResume() 中执行此操作,此方法的缺点是每次活动进入前台时都会重新加载光标。 。
希望这能说明白
Again the solution depends on what suits you.
If you want the cursor to be pre-populated once per application (and not bothered about any change, then you can do it in onCreate(). This method will be recalled only if the app process is killed and app is reinitiated.
If you want the cursor to be prepopulated everytime the visible lifetime starts (most cases a service/broadcast is calling your activity, you should use onStart()
If you want the cursor to be prepopulated for every foreground lifecyle of activity, you should use onResume(). So if you have a dialog box or another subactivity modifying some information and hence you want to reload the cursor, it is best you do so in onResume(). The downside for this method is everytime the activity comes in foreground the cursor is reloaded.
Hope this makes it clear
要回答有关 NoteEditor 的问题,只需看一下您引用的那一行上方的行,您就会看到...
该评论似乎解释了这一切。虽然我自己没有完成 NotePad 示例,但作者似乎正在构建在 NoteEditor 暂停(然后恢复)时从更改中恢复的能力。
正如 GSree 所解释的(当我输入此内容时),没有正确或错误的答案,它仅取决于 Activity 生命周期的哪个点需要完成什么操作。
To answer your question about NoteEditor, simply take a look at the lines above the one you cite and you'll see...
The comment seems to explain it all. Although I haven't gone through the NotePad example myself, it appears the author(s) are building in the ability to recover from changes whilst the NoteEditor is paused (and then resumed).
As GSree explains (whilst I was typing this), there isn't a right or wrong answer and it simply depends on what needs to be done at which point of the Activity life-cycle.