注册 | 登录

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

itPublisher 分享于



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?… –  Chuck D Jan 8 '13 at 5:30


2 Answers

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 = ... ;
    new ViewTreeObserver.OnGlobalLayoutListener() {

    public void onGlobalLayout() {
        } else {

        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









您的注册邮箱: 修改

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