Learn more about Android View life cycle

Source: Internet
Author: User
Tags gety

As a basis for customizing the view, if you don't understand the life cycle of Android view, you'll find such a problem in later maintenance ....

For a while. The students of Android development know that the general OnXXX function is the callback function of the system. And this blog is based on this idea (perhaps a bit stupid) ...

Let's start with three points. Create a view's log information (custom view configuration into an XML file):

Android:visibility=gone

03-25 19:56:55.934:d/yyyyy (11493): onvisibilitychanged--------=====03-25 19:56:55.934:d/yyyyy (11493): Construct 2 Parameters. 03-25 19:56:55.934:e/yyyyy (11493): onfinishinflate03-25 19:56:55.934:d/yyyyy (11493): onvisibilitychanged--------=====03-25 19:56:55.934:d/yyyyy (11493): onvisibilitychanged--------=====03-25 19:56:55.944:D/YYYYY (11493): onrtlpropertieschanged--------=====03-25 19:56:55.954:d/yyyyy (11493): onrtlpropertieschanged--------=====03-25 19:56:55.954:e/yyyyy (11493): onattachedtowindow03-25 19:56:55.954:d/ YYYYY (11493): onwindowvisibilitychanged--------=====03-25 19:56:55.974:d/yyyyy (11493): onwindowfocuschanged--------=====


Android:visibility=invisible

03-25 19:57:38.204:d/yyyyy (11694): onvisibilitychanged--------=====03-25 19:57:38.204:d/yyyyy (11694): Construct 2 Parameters. 03-25 19:57:38.204:e/yyyyy (11694): onfinishinflate03-25 19:57:38.204:d/yyyyy (11694): onvisibilitychanged--------=====03-25 19:57:38.204:d/yyyyy (11694): onvisibilitychanged--------=====03-25 19:57:38.224:D/YYYYY (11694): onrtlpropertieschanged--------=====03-25 19:57:38.224:d/yyyyy (11694): onrtlpropertieschanged--------=====03-25 19:57:38.224:e/yyyyy (11694): onattachedtowindow03-25 19:57:38.224:d/ YYYYY (11694): onwindowvisibilitychanged--------=====03-25 19:57:38.224:d/yyyyy (11694): Onmeasure, width:1080; height:155703-25 19:57:38.224:d/yyyyy (11694): Onmeasure, width:144; height:150003-25 19:57:38.234:d/yyyyy (11694): onsizechanged03-25 19:57:38.234:i/yyyyy (11694): onLayout--l:0; r:144; t:57; b:201:changed:true03-25 19:57:38.254:d/yyyyy (11694): Onmeasure, width:1080; height:155703-25 19:57:38.254:d/yyyyy (11694):Onmeasure, width:144; height:150003-25 19:57:38.254:i/yyyyy (11694): onlayout--l:0; r:144; t:57; b:201:changed:false03-25 19:57:38.264:d/yyyyy (11694): onwindowfocuschanged--------=====


Android:visibility=visible

03-25 19:55:15.434:d/yyyyy (11304): Construct 2 parameters. 03-25 19:55:15.434:e/yyyyy (11304): onfinishinflate03-25 19:55:15.434:D/YYYYY (11304): onvisibilitychanged--------=====03-25 19:55:15.434:d/yyyyy (11304): onvisibilitychanged--------=====03-25 19:55:15.454:d/yyyyy (11304): onrtlpropertieschanged--------=====03-25 19:55:15.454:D/YYYYY (11304): onrtlpropertieschanged--------=====03-25 19:55:15.454:e/yyyyy (11304): onattachedtowindow03-25 19:55:15.454:d/yyyyy (11304): onwindowvisibilitychanged--------=====03-25 19:55:15.454:d/ YYYYY (11304): Onmeasure, width:1080; height:155703-25 19:55:15.454:d/yyyyy (11304): Onmeasure, width:144; height:150003-25 19:55:15.464:d/yyyyy (11304): onsizechanged03-25 19:55:15.464:i/yyyyy (11304): onLayout--l:0; r:144; t:57; b:201:changed:true03-25 19:55:15.474:d/yyyyy (11304): Onmeasure, width:1080; height:155703-25 19:55:15.474:d/yyyyy (11304): Onmeasure, width:144; height:150003-25 19:55:15.474:i/yyyyy (11304): OnLayout---l:0; r:144; t:57; b:201:changed:false03-25 19:55:15.474:d/yyyyy (11304): OnDraw--------=====03-25 19:55:15.484:d/yyyyy (11304): OnWind owfocuschanged--------=====


1, it is not difficult to see the default view is visible, not the default value when the first call onvisibilitychanged, but at this point in the view of any location information is not known.

2. A constructor with two parameters is called after visibility changes

3. Inflate complete from XML file

4. Add View to Window (view is gone, then the view creation life cycle ends)

5. Measuring the length and width of the view (onmeasure)

6. Position view in Parent view (onlayout)--(view is invisible, end of view creation life cycle)

7. OnDraw (only visible View is drawn in window)

To construct a view in your code:

Setcontentview (New Cusview (this)) Enter the log information as follows:

03-25 20:37:51.284:e/yyyyy (12530): Construct 1 parameter03-25 20:37:51.294:d/yyyyy (12530): onvisibilitychanged--------=====03-25 20:37:51.314:d/yyyyy (12530): onvisibilitychanged--------=====03-25 20:37:51.314:D/YYYYY (12530): onrtlpropertieschanged--------=====03-25 20:37:51.314:d/yyyyy (12530): onrtlpropertieschanged--------=====03-25 20:37:51.314:e/yyyyy (12530): onattachedtowindow03-25 20:37:51.314:d/ YYYYY (12530): onwindowvisibilitychanged--------=====03-25 20:37:51.314:d/yyyyy (12530): Onmeasure, width:1080; height:155703-25 20:37:51.314:d/yyyyy (12530): onsizechanged03-25 20:37:51.324:i/yyyyy (12530): onLayout--l:0; r:1080; t:0; b:1557:changed:true03-25 20:37:51.324:d/yyyyy (12530): Onmeasure, width:1080; height:155703-25 20:37:51.324:i/yyyyy (12530): onlayout--l:0; r:1080; t:0; b:1557:changed:false03-25 20:37:51.324:d/yyyyy (12530): OnDraw--------=====03-25 20:37:51.344:d/yyyyy (12530): OnWin dowfocuschanged--------=====

From the test results, by default, the length and width of the view is the same as the length and width of the parent view.

The OnDraw function is called, but nothing is visible on the screen, what is the reason?
When you don't see anything, check that the view you want to draw is set up.

Why did I specify the layoutparameters, but not the effect?

Specifying layoutparameters in an inappropriate life cycle is ignored, such as the following code:

@Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        view = new Cusview (this);        View.setimageresource (r.drawable.ic_launcher);        Framelayout.layoutparams params = new Framelayout.layoutparams (.);        View.setlayoutparams (params);        Setcontentview (view);    }

The correct method should be to put the Onwindowfocuschanged method to the focus and then specify Layoutparameters, the following code:

@Override public    void Onwindowfocuschanged (Boolean hasfocus) {        //TODO auto-generated method stub        Super.onwindowfocuschanged (hasfocus);        if (hasfocus) {            view.setimageresource (r.drawable.ic_launcher);            Framelayout.layoutparams params = new Framelayout.layoutparams (.);            View.setlayoutparams (params);        }    }


Why do I specify the layoutparameters parameter times exception? The exception information is as follows:

Java.lang.classcastexception:android.view.viewgroup$layoutparams cannot is cast to android.view.viewgroup$ Marginlayoutparams

Java.lang.Object
? Android.view.ViewGroup.LayoutParams
? Android.view.ViewGroup.MarginLayoutParams

Layoutparameters parameter type is not correct, from the above inheritance relationship can see Marginlayoutparameters expanded ViewGroup layoutparameters, Modify it to any layoutparameters that supports margin action.

Next we'll look at three copies of the view's destruction log:

Android:visibility=visible

03-25 21:15:35.404:d/yyyyy (14589): onwindowfocuschanged--------=====03-25 21:15:35.484:d/yyyyy (14589): onwindowvisibilitychanged--------=====03-25 21:15:35.504:d/yyyyy (14589): Ondetachedfromwindow--------=====

Android:visibility=gone

03-25 21:16:09.964:d/yyyyy (14736): onwindowfocuschanged--------=====03-25 21:16:10.054:d/yyyyy (14736): onwindowvisibilitychanged--------=====03-25 21:16:10.064:d/yyyyy (14736): Ondetachedfromwindow--------=====

Android:visibility=invisible

03-25 21:16:42.534:d/yyyyy (14860): onwindowfocuschanged--------=====03-25 21:16:42.594:d/yyyyy (14860): onwindowvisibilitychanged--------=====03-25 21:16:42.614:d/yyyyy (14860): Ondetachedfromwindow--------=====


As you can see from the above, the visibility attribute has no effect on the view's destruction process.

In summary: The key life cycle for view is [change visibility]-structure view---onfinishinflate--and Onattachedtowindow-Onmeasu Re----onsizechanged--onlayout---OnDraw Ondetackedfromwindow

Finally, a small piece of code is used to drag the view on the screen (by modifying the layout of the view):

Private float Mdownx, Mdowny, x, y;    private int dx, DY, IL, IR, it, IB;    @Override Public    Boolean ontouchevent (Motionevent event) {        x = Event.getx ();        y = event.gety ();        Switch (event.getaction ()) {case            motionevent.action_down:                mdownx = Event.getx ();                Mdowny = Event.gety ();                Il = GetLeft ();                IR = GetRight ();                it = GetTop ();                IB = Getbottom ();                break;            Case Motionevent.action_move: Case            motionevent.action_up:                dx + = Math.Round (X-MDOWNX);                Dy + = Math.Round (y-mdowny);                Layout (il + dx, it + dy, ir + dx, ib + dy);                break;        }        return true;    }

Learn more about Android View life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.