Android的viewDidLoad和viewDidAppear等价

发布于 2024-09-15 02:18:01 字数 162 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱 2024-09-22 02:18:01

Activity 类具有 onCreate 和 onResume 方法,与 viewDidLoad 和 viewDidAppear 非常相似。

Activity.onResume

编辑

除此之外,由于有些人在评论中提到视图树在这些回调期间尚未完全可用,因此如果您需要首先访问视图层次结构,您可以监听 ViewTreeObserver 。以下是如何使用 ViewTreeObserver 来实现此目的的示例。

    View someView = findViewById(R.id.someView);
    final ViewTreeObserver obs = someView.getViewTreeObserver();
    obs.addOnPreDrawListener(new OnPreDrawListener() {

        public boolean onPreDraw() {
            obs.removeOnPreDrawListener(this);
            doMyCustomLogic();
            return true;
        }
    });

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.

    View someView = findViewById(R.id.someView);
    final ViewTreeObserver obs = someView.getViewTreeObserver();
    obs.addOnPreDrawListener(new OnPreDrawListener() {

        public boolean onPreDraw() {
            obs.removeOnPreDrawListener(this);
            doMyCustomLogic();
            return true;
        }
    });
却一份温柔 2024-09-22 02:18:01

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.

孤独患者 2024-09-22 02:18:01

根据我对 Android 的有限、初步了解,您可以在 onCreate 方法 活动

onCreate(Bundle) 就是你的地方
初始化您的活动。最多
重要的是,在这里你通常会
使用布局调用 setContentView(int)
定义您的 UI 的资源,并使用
findViewById(int) 检索
您需要的 UI 中的小部件
以编程方式进行交互。

viewDidAppear 的等效项更接近于 onResume 方法:

之后调用
onRestoreInstanceState(Bundle),
onRestart() 或 onPause(),为您
活动开始与
用户。这是一个很好的起点
动画,开放独占访问
设备(如相机)等

From my limited, nascent understanding of Android, you implement viewDidLoad type functionality in the onCreate method of your Activity:

onCreate(Bundle) is where you
initialize your activity. Most
importantly, here you will usually
call setContentView(int) with a layout
resource defining your UI, and using
findViewById(int) to retrieve the
widgets in that UI that you need to
interact with programmatically.

The equivalent for viewDidAppear is closer to the onResume method:

Called after
onRestoreInstanceState(Bundle),
onRestart(), or onPause(), for your
activity to start interacting with the
user. This is a good place to begin
animations, open exclusive-access
devices (such as the camera), etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文