Android Development--ui_ Clips

Source: Internet
Author: User

        • Create a fragment
          • Fragment class
          • Add a fragment to an activity
        • Communication between activities and events
        • The life cycle of a fragment

Fragments are designed to provide more flexible UI support for large screens, such as tablets. It can be thought of as a child activity, must be embedded in the activity, and has its own life cycle, can receive their own user input events. When you add a clip as part of the active layout, the clip defines its own view layout. How do I add a fragment to an active layout file? The fragment is used as a component of the activity through the tag.

Each fragment should be designed as a reusable modular component that avoids manipulating another fragment directly from one segment, allowing a fragment to be added to multiple activities. The modular component makes it possible to adaptively select a single-window UI or a multi-window UI depending on the size of the screen. As an example of a news application, a tablet can add a list fragment and a content fragment to a single activity, while in a mobile phone, there is only a list fragment in an activity, and the activity that contains the content fragment appears after the click.

Create a fragment Fragment Class

Custom fragments must inherit from fragment. Creating a fragment generally implements at least the following life cycle methods:

    • OnCreate (): This method is called when the system creates a fragment. The component that you want to persist when the fragment is paused or stopped is expected to be initialized in this method.

    • Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate): This method loads its layout file for a fragment, The system calls this method when it needs to draw the fragment layout.
      Parameters:
      Inflater: for loading layout files
      Container: The parent viewgroup that the fragment layout will be inserted in, that is, the active layout.
      Savedinstancestate: A bundle that provides data about an instance of a segment when recovering a fragment

Example

publicstaticclass ExampleFragment extends Fragment {    @Override    publiconCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        returnfalse);    }}
    • OnPause (): Any changes that are still valid after the end of the user session should be confirmed within this method.
add a fragment to an activity
    • Method One: Declare the fragment in the active layout file
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:orientation="Horizontal"android:layout_width="Match_parent"  Android:layout_height="Match_parent">                <fragment  android:name  = "com.example.news.ArticleListFragment"  and Roid:id  = "@+id/list"  android:layout_weight
      = "1"  android:layout_width  = "0DP"  android:layout_height  = "m Atch_parent "/>     <fragment android:name="Com.example.news.ArticleReaderFragment"android:id ="@+id/viewer"android:layout_weight= "2"android:layout_width="0DP"  android:layout_height="match_parent" />                                                </linearlayout>

When the system creates this layout, each fragment specified in the layout is instantiated, and the Oncreateview () method is called for each fragment to load the layout of each fragment. The system replaces < fragment > elements with the returned view.

    • Method Two: Dynamically add fragments to an existing viewgroup in the source code

To perform fragment transactions in an activity, such as adding, deleting, and replacing fragments, you must use Fragmenttransaction:

FragmentManager fragmentManager = getFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

To add a fragment:

ExampleFragment fragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container, fragment);#第一个参数为片段插入到的视图fragmentTransaction.commit();
communication between activities and events

The activity instances associated with the fragment can be obtained by getactivity () in the Fragment class;
You can get the fragment instance through Fragmentmanager.findfragmentbyid () in the Activity class:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
the life cycle of a fragment

The life cycle of a fragment is similar to the life cycle of the activity, with the biggest difference being that the activity is stopped when it is put into a system-managed activity return stack, while the fragment is explicitly called Addtobackstack () during the removal of the transaction execution, and the fragment is placed in the return stack managed by the activity.
In addition to the same life cycle callback method as the activity, there are several additional callback methods for the fragment:
Onattach (): Called when a fragment is associated with an activity;
Oncreateview (): Called when creating a view for a fragment
Onactivitycreated (): Called when the active OnCreate () method has been returned
Ondestroyview (): Called when the view associated with the fragment is removed
Ondetach (): Called when the fragment is associated with an activity.

Android Development--ui_ Clips

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.