Android的生命周期:在数据填写活动在onStart()或onResume()? [英] Android lifecycle: Fill in data in activity in onStart() or onResume()?
问题描述
的如果您通过光标获取数据,并在屏幕上的数据,如(设置窗口标题,在在onStart)
或<$ C填写$ C> onResume()
在onStart()
似乎逻辑的地方,因为在在onStart()
活动已经可以显示,尽管在后台运行。值得注意的是我有一个问题管理对话框,使我重新考虑这一点。如果用户旋转屏幕,同时对话框仍然打开, onCreateDialog()
和上prepareDialog()
被称为的 在onStart()
和 onResume()
。如果必须基于数据的对话框,你需要有之前 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()
.
如果我对在onStart正确的是()
那么为什么在记事本的例子在<$ C $这样做给了一个坏榜样C> onResume()?见<一href=\"http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html\">http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html NoteEditor.java线176(标题= mCursor.getString ...
)。
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...
).
另外,如果我的活动启动另一Actvity /对话框,它改变了我的光标正在跟踪的数据。即使在最简单的情况,这是否意味着我必须手动的更新我的previous屏幕(在主要活动对话框侦听器),或者说我必须要注册一个ContentObserver,因为我不再更新的onResume()(虽然我两次,当然更新)?
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.
推荐答案
要解答有关问题NoteEditor,只需看看你举一个以上的线,你会看到...
To answer your question about NoteEditor, simply take a look at the lines above the one you cite and you'll see...
// Requery in case something changed while paused (such as the title)
mCursor.requery();
注释似乎可以解释这一切。虽然我没有通过记事本的例子了自己,它出现在作者(S)正在建设中,从变化虽然NoteEditor暂停(然后再恢复)的恢复能力。
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).
由于GSree解释(虽然我打字本),有不正确或错误的答案,它只是依赖于需要在活动生命周期的这点做的事情。
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.
这篇关于Android的生命周期:在数据填写活动在onStart()或onResume()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!