Android的viewDidLoad和viewDidAppear等价
Android 是否有相当于 Cocoa 的 viewDidLoad 和 viewDidAppear 函数?
如果没有,那么当视图出现时我将如何执行操作?我的应用程序是一个选项卡式应用程序,其中一个选项卡是论坛主题列表。我希望每次出现视图时刷新主题列表。 Android 中可能有这样的事情吗?
Does Android have an equivalent to Cocoa's viewDidLoad and viewDidAppear functions?
If not, then how would I go about performing an action when a View appears? My app is a tabbed application, in which one of the tabs is a list of forum topics. I would like the topic list to be refreshed every time the view appears. Is such a thing possible in Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Activity 类具有 onCreate 和 onResume 方法,与 viewDidLoad 和 viewDidAppear 非常相似。
Activity.onResume
编辑
除此之外,由于有些人在评论中提到视图树在这些回调期间尚未完全可用,因此如果您需要首先访问视图层次结构,您可以监听 ViewTreeObserver 。以下是如何使用 ViewTreeObserver 来实现此目的的示例。
The Activity class has onCreate and onResume methods that are pretty analagous to viewDidLoad and viewDidAppear.
Activity.onResume
EDIT
To add to this, since some have mentioned in the comments that the view tree is not yet fully available during these callbacks, there is the ViewTreeObserver that you can listen to if you need first access to the view hierarchy. Here is a sample of how you can use the ViewTreeObserver to achieve this.
onResume() 更像是 viewCouldAppear。 :) public void onWindowFocusChanged(boolean) 最接近 viewDidAppear。此时,在活动生命周期内,您可以向视图询问其大小。
onResume() is more like viewCouldAppear. :) public void onWindowFocusChanged(boolean) is the closest to viewDidAppear. At this point within the activity lifecycle you may ask the view about its size.
根据我对 Android 的有限、初步了解,您可以在 onCreate 方法 活动:
viewDidAppear 的等效项更接近于 onResume 方法:
From my limited, nascent understanding of Android, you implement viewDidLoad type functionality in the onCreate method of your Activity:
The equivalent for viewDidAppear is closer to the onResume method: