ITKeyword,专注技术干货聚合推荐

注册 | 登录

Android Fragments Lifecycle: How can I do something when the views have been drawn?

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

I was under the impression from my experience with normal activities that in the onResume() function you could perform actions on the views, and that they would have been rendered to the screen already.

However, I now converted some of my code to a Fragment and in the onResume() of the fragment when I call a view.getWidth() it returns 0 (the view is created in onCreateView). The view does get rendered eventually, but I can't seem to access it in the lifecycle of the fragment.

Is this normal?

android android-fragments lifecycle
|
  this question
asked Jan 7 '13 at 22:19 Peterdk 7,254 11 75 111      you should call getView() instead and based on the lifecycle I would say that the view should be accessible in onResume(). –  A--C Jan 7 '13 at 22:22      Yes, I use getView() to get it, but they just return 0 widths in onResume(). Very weird. –  Peterdk Jan 7 '13 at 22:27      Can you use onViewCreated? developer.android.com/reference/android/app/… –  Chuck D Jan 8 '13 at 5:30

 | 

2 Answers
2

onResume is called when the Fragment comes back to the foreground, but it does not guarantee that it is visible yet or that the layout has already happened. A Fragment's onResume is tied to its Activity - here is an extract from the documentation:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

The best way to get the size of a view would be through a ViewTreeObserver.OnGlobalLayoutListener. It is called once the layout is complete and you can safely get the size of a view from there.

Simply register an OnGlobalLayoutListener on the view your are interested in and get its size once the layout has happened. You should unregister your listener once you have read the value and do not care about any further updates. (The listener is called multiple times otherwise.)

View v = ... ;
...
v.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {

    @SuppressWarnings("deprecation")                                                
    @SuppressLint("NewApi")
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }

        height = v.getHeight();
        width = v.getWidth();
    }
});

(Note the compatibility check - the method was renamed in Jelly Bean.)


|
  this answer
answered Jan 8 '13 at 5:23 Jan-Felix Schmakeit 76 1

 | 

The view's dimensions are known after the whole window has been drawn. Since the fragment is usually a child view contained in the host activity layout, the fact that the child view is created in your fragment does not mean it is added to the parent's ViewGroup yet.


|
  this answer
answered Jan 7 '13 at 22:33 znat 5,923 10 38 64      But shouldn't it be added already in onResume()? Isn't there a way to run code after the view has been created? –  Peterdk Jan 7 '13 at 23:42      I don't see why it should be guaranteed but I am not 100% sure. You could check with the debugger: create a breakpoint in onResume() and check is view.getParent() is null –  znat Jan 8 '13 at 1:46

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。