Android06-fragment fragments

Source: Internet
Author: User

¨fragment Profile ¨fragment Lifecycle ¨ dynamic load fragmentation Fragment Manager uses 1.Fragment to represent an activity or part of the user interface. Fragment can be seen as a modular unit in activity that can be reused in different activity
inflater.inflate (R.layout.right_fragment, container, false); return view;} }

Three parameters of the inflate method:

¨ the Id¨viewgroup type object of the layout file, fragment the container object (such as a layout object) ¨ Boolean type 2. Adding fragments dynamically Use Framelayout in the active layout to replace framelayout with required fragmentation at run time

Dynamic addition of fragments is mainly divided into 5 steps.

  1. Creates a fragment instance to be added.

  2. Gets to Fragmentmanager, which can be directly called by the Getfragmentmanager () method in the activity.

  3. Opens a transaction, which is opened by calling the BeginTransaction () method.

  4. Adding fragments to a container, typically implemented using the Replace () method, requires the ID of the incoming container and the fragment instance to be added.

  5. Transaction, call the Commit () method to complete.

  6. public void OnClick (View v) {            switch (V.getid ()) {            R.id.button:                anotherrightfragment fragment = new Anot Herrightfragment ();                Fragmentmanager Fragmentmanager = Getfragmentmanager ();                Fragmenttransaction transaction = Fragmentmanager.    BeginTransaction ();                Transaction.replace (r.id.right_layout, fragment);                Transaction.commit ();                break;            Default: Break                ;}}

    Management fragment

    Use the Fragmentmanager class to manage fragment, using the Getfragmentmanager method of activity to get Fragmentmanager objects

      

  7. The life cycle of the
  8. fragment is  

    1. Running state when a fragment is visible and the activity it is associated with is running, the fragment is also running.

    2. Paused state when an activity enters a paused state (because another activity that does not occupy the screen is added to the top of the stack), the visible fragments associated with it go into a paused state.

    3. Stop status

    When an activity enters a stopped state, the fragments associated with it go to the stopped state. or by calling Fragmenttransaction's remove (), replace () method to remove the fragment from the activity, but with the Addtobackstack () method called before the transaction intersection, the fragment also goes to the stop state. In general, fragments entering the stop state are completely invisible to the user and may be reclaimed by the system.
    4. Destroy state

    Fragmentation is always dependent on activity, so when the activity is destroyed, the fragments associated with it are entered into the destruction state. or by calling Fragmenttransaction's remove (), replace () method to remove the fragment from the activity, but before the transaction intersection, the Addtobackstack () method is not called, and the fragment will go to the destroy state.  

    1. Onattach ()

       called when fragmentation is associated with an activity. Called when 
    2. Oncreateview ()

       creates a view (load layout) for fragmentation. 
    3. onactivitycreated ()

       ensure that the activity associated with the fragment must have been created. 
    4. Ondestroyview ()

       called when the view associated with the fragment is removed. 
    5. Ondetach ()

       called when fragmentation and activity are disassociate. 

Android06-fragment fragments

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.