Android生命周期:在onStart()或onResume()中填写活动中的数据? [英] Android lifecycle: Fill in data in activity in onStart() or onResume()?

查看:321
本文介绍了Android生命周期:在onStart()或onResume()中填写活动中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您通过游标获取数据并填写屏幕上的数据,例如设置窗口标题,请在 onStart() onResume()



onStart()似乎是合乎逻辑的地方,因为在 onStart()之后,活动已经可以显示,尽管在​​后台。值得注意的是,我遇到了一个管理对话框的问题,使我重新考虑了这一点。如果用户在对话框仍然打开的情况下旋转屏幕,则$ onCreateDialog()和 onPrepareDialog()称为 c $ c $ onCount() onResume()。如果对话框需要基于数据,您需要在 onResume()之前拥有数据。



如果关于 onStart()是正确的,那么为什么记事本例子 onResume( )?请参阅 http://developer.android。 com / resources / samples / NotePad / src / com / example / android / notepad / NoteEditor.html NoteEditor.java第176行( title = mCursor.getString ... )。



此外,如果我的活动启动另一个Actvity / Dialog,更改我的光标跟踪的数据,该怎么办?即使在最简单的情况下,这是否意味着我必须手动更新我的上一个屏幕(主要活动中的对话框的监听器),或者我必须注册一个ContentObserver,因为我' m不再更新onResume()中的数据(虽然我可以更新它当然是两次)?



我知道这是一个基本的问题,但最近才对话到我的惊喜,让我意识到这一点。

解决方案

要回答您关于NoteEditor的问题,只需查看一个你上面的行引用,你会看到...

  //要求暂停时更改某些内容(如标题)
mCursor.requery();

评论似乎解释了一切。虽然我本人还没有经过笔记本电脑的例子,但似乎笔者正在修改NotesEditor暂停恢复的功能(然后重新开始)。


$ b $由于GSree解释(当我打字时),没有一个正确或错误的答案,它只取决于在活动生命周期的哪一点需要做什么。


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.

解决方案

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();

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.

这篇关于Android生命周期:在onStart()或onResume()中填写活动中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆