How to use Android fragment and precautions

Source: Internet
Author: User
Tags commit
1. Use Support Library

If your application needs to run in versions 3.0 and above, you can ignore this part of the content.

If your application uses a version below 3.0, 1.6, or more, you need to use a support library to build it.

To use the Support library:



  1. Download the Android Support Package using the SDK Manager tool under the SDK


    2. Create a libs directory under the top-level directory of your Android project

    3. Locate the/extras/android/support/v4/android-support-v4.jar under your SDK and copy it to the Libs of your project, select this jar package → right button →build path→add to build P Ath

    4.在您的项目的 Manifest.xml 文件的 <manifest> 标签下添加:

    < uses-sdk android:minSdkVersion = "4"

    android:targetsdkversion = "8" />

      其中 targetSdkVersion 是您的软件最小支持的版本

    5. If your project supports the version below 3.0, please import the following package: android.support.v4.*;

    In the use of fragment activity please inherit fragmentactivity rather than activity. If your system is 3.0 or older, you will also need to import similar packages, but you can use normal activity.


    2. Create a Fragment

    Fragment support is used in different activity and can handle its own input events and lifecycle methods. Can be seen as a child activity.

    Create a Fragment

    Creating a fragment is similar to creating an activity, inheriting the fragment class, overriding the lifecycle approach, and the main difference is the need to rewrite a Oncreateview () method to return the fragment layout. Example:




    The fragment lifecycle approach relies on the life cycle of an activity, such as when the life cycle method of an activity OnPause () is invoked, all the Fragment OnPause () methods in this activity are also invoked.


    For more information please refer to class fragment.

    Use XML Add Fragment to the Activity

    Although fragment can be reused by multiple activity, you must also associate fragment to a fragmentactivity. You can use the XML layout file to implement this association.

    Description: The above mentioned fragmentactivity is applicable to the version of API below 3.0, and 3.0 and above versions can use ordinary activity.

    Example:



    Using the fragment tag above, android:name= "" specifies a fragmentthat is added to the XML . For more information on creating different screen size layouts, please read support for different screen sizes.





    When you add fragments to a layout XML file defined by an active layout, you cannot delete fragments at run time. If you intend to interact with the user and swap fragments, you must add active fragments to the activity when the first start.


    3. Build a flexible UI


    Fragmentmanager provides an action to add, delete, and replace fragment when the activity is running.


    You can add fragment instead of defining them in an XML layout file while the activity is running. If you intend to change the life of fragment in an activity.


    If you want to perform additions, deletions, and modifications, you must obtain a Fragmenttransaction object through the Fragmentmanager object, and use its APIs to perform these operations.


    To add a fragment to an activity, you must add this fragment to a container view. Example:



    In the activity you can get the fragment object by Getfragmentmanager () and then through the beginfragmenttransaction of the Fragmentmanager object () method to obtain the Fragmenttransaction object. Add a fragment to the current activity through its add () method.

    A Fragmenttransaction object can perform multiple additions and deletions, and if you want to commit these changes to an activity, you must call the object's commit () method at the end. Example:





    Because it is not defined in an XML layout, all operations that can transform deletes and modifications.

    If you replace or delete a fragment and then allow the user to navigate to the previous fragment, you must call the Addtobackstack () method before calling the commit () method to add to the fallback stack. If you add this fragment to the fallback stack, the fragment will be stop instead of destroyed after the submission. If the user navigates to the fragment, the fragment is restart rather than recreated. If you do not add it to the fallback stack, it will be destroyed when it is deleted or replaced. Example:



    4. Interaction with other fragment

    There should be no communication between the two individual fragment. They should use the activity of their existence as a communication link.

    To implement two fragment interactions, you can define an interface in fragment and then define a method in the interface that invokes the method in the Fragment Onattach () method. Then let the activity implement this method to complete the communication between the activity and the fragment. Example:

    To define an interface and invoke the method:




    Implements an interface in which you can interact with other fragment data:



    You can find a fragment by Fragmentmanager's Findfragmentbyid ().



    Fragment use attention

  2. Before saying this, need to talk about, fragment layout principle, fragment layout is called layer layout, is a layer of cover one layer. When a layout requires multiple fragment layout nesting. You will encounter this sad situation, you will find two of fragment content overlap to a piece. If there are buttons in the page, you will find the button on the first page of the point. But the button click event on the second page is also triggered.

  3. This is the situation that met yesterday, the following talk about solving the hair, first of all you have to find your fragment layout file.

  4. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
    Android:layout_width= "Match_parent"
    android:layout_height= "Match_parent"
    android:background= "@color/white"
    Android:clickable= "true"
    android:orientation= "Vertical" >

    <relativelayout
    Android:layout_width= "Match_parent"
    android:layout_height= "Wrap_content"
    android:layout_margintop= "20DP" >

    <imageview
    Android:id= "@+id/iv_center_head"
    Android:layout_width= "50DP"
    android:layout_height= "50DP"
    android:layout_marginleft= "10DP"
    android:background= "@drawable/ic_launcher"/>

    <textview
    Android:id= "@+id/tv_center_shop_name"
    Android:layout_width= "Wrap_content"
    android:layout_height= "Wrap_content"
    android:layout_margintop= "9DP"
    android:layout_torightof= "@+id/iv_center_head"
    Android:text= "ASD"
    Android:textsize= "14sp"/>
    </RelativeLayout>
    </LinearLayout>

  5. This is a layout file, which is exactly the fragment layout file, which needs to be noted:


  6. android:background= "@color/white"
    Android:clickable= "true"

  7. These two lines of code, for fragment set the background, in case two fragment layout files overlap, content disorder appears. The second line of code prevents the tap from penetrating. There is also a final recommendation not to use multiple fragment reuse.

Related Article

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.