2.App Components-Activities/Fragments

1. Fragments

  A Fragment represents a behavior or a portion of user interface in an Activity.

  You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a

    fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove

    while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

  A fragment must always be embedded in an activity and the fragment‘s lifecycle is directly affected by the host activity‘s lifecycle

  

2. Design Philosophy

  For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the

     right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle

     their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can

     select an article and read it all within the same activity, as illustrated in the tablet layout in figure 1.

    

  

3. Creating a Fragment

    

  There are also a few subclasses that you might want to extend, instead of the base Fragment class:

DialogFragment
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment.
ListFragment
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events.
PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a "settings" activity for your application.

4. Adding a user interface

  To provide a layout for a fragment, you must implement the onCreateView() callback method, which the Android system calls when it‘s time

    for the fragment to draw its layout. Your implementation of this method must return a View that is the root of your fragment‘s layout.

  To return a layout from onCreateView(), you can inflate it from a layout resource defined in XML. To help you do so, onCreateView() provides

    a LayoutInflater object

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

  The container parameter passed to onCreateView() is the parent ViewGroup (from the activity‘s layout) in which your fragment layout will be

    inserted.

  The savedInstanceState parameter is a Bundle that provides data about the previous instance of the fragment, if the fragment is being resumed

  The inflate() method takes three arguments:

    <1>The resource ID of the layout you want to inflate

    <2>The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout

      parameters to the root view of the inflated layout, specified by the parent view in which it‘s going.

    <3>A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation.

      (In this case, this is false because the system is already inserting the inflated layout into the container—passing true would create a

      redundant view group in the final layout.)

5. Adding a fragment to an activity

  Usually, a fragment contributes a portion of UI to the host activity, which is embedded as a part of the activity‘s overall view hierarchy.

    There are two ways you can add a fragment to the activity layout:

  <1>Declare the fragment inside the activity‘s 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"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_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>

    The android:name attribute in  <fragment> specifies the Fragment class to instantiate in the layout

    When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each

      one, to retrieve each fragment‘s layout. The system inserts the View returned by the fragment directly in place of the <fragment> element.

  <2>Or progammatically add the fragment to an existing ViewGroup   

    At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to

      place the fragment. To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs

      from FragmentTransaction. You can get an instance of FragmentTransaction from your Activity like this:

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

    You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

   

6. Managing Fragments

  To manage the fragments in your activity, you need to use FragmentManager. To get it, call getFragmentManager() from your activity.

7. Performing Fragment Trasactions

  A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response

    to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in

    FragmentTransaction. You can also save each transaction to a back stack managed by the activity, allowing the user to navigate backward

    through the fragment changes (similar to navigating backward through activities).

  Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a

    given transaction using methods such as add(), remove(), and replace(). Then, apply the transaction to the activity, you must call commit().

  For example, here‘s how you can replace one fragment with another, and preserve the previous state in the back stack: 

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

  

8. Communicating with the Activity

  Although a Fragment is implemented as an object that‘s independent from an Activity and can be used inside multiple activities, a given

    instance of a fragment is directly tied to the activity that contains it.

View listView = getActivity().findViewById(R.id.list);

  

9. Creating event callback  to the activity

  In some cases, you might need a fragment to share events with the activity. A good way to do that is to define a callback interface inside the

    fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the

    information with other fragments in the layout as necessary.

  For example, if a news application has two fragments in an activity—one to show a list of articles (fragment A) and another to display an

    article (fragment B)—then fragment A must tell the activity when a list item is selected so that it can tell fragment B to display the article.

    In this case, the OnArticleSelectedListener interface is declared inside fragment A:

public static class FragmentA extends ListFragment {
    ...
    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);
    }
    ...
}

  Then the activity that hosts the fragment implements the OnArticleSelectedListener interface and overrides onArticleSelected() to notify

    fragment B of the event from fragment A.

  To ensure that the host activity implements this interface, fragment A‘s onAttach() callback method (which the system calls when adding the

     fragment to the activity) instantiates an instance of OnArticleSelectedListener by casting the Activity that is passed into onAttach():

public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
    ...
}

  If the activity has not implemented the interface, then the fragment throws a ClassCastException

  For example, if fragment A is an extension of ListFragment, each time the user clicks a list item, the system calls onListItemClick() in the

    fragment, which then calls onArticleSelected() to share the event with the activity 

public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item‘s row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(noteUri);
    }
    ...
}

10. Fragment Lifecycle

    

11. Examples

  To bring everything discussed in this document together, here‘s an example of an activity using two fragments to create a two-pane layout.

    The activity below includes one fragment to show a list of Shakespeare play titles and another to show a summary of the play when

    selected from the list. It also demonstrates how to provide different configurations of the fragments, based on the screen configuration.

  

时间: 2024-10-12 15:43:21

2.App Components-Activities/Fragments的相关文章

Android官方文档之App Components(Fragments)

Fragment是Android API中的一个类,它代表Activity中的一部分界面:您可以在一个Activity界面中使用多个Fragment,或者在多个Activity中重用某一个Fragment. 本文将介绍Fragment的定义.创建.添加.移除.生命周期 等,如需访问官方原文,您可以点击这个链接:<Fragments>. Fragments 可以把Fragment想象成Activity中的一个模块,它有自己的生命周期.可以接收输入事件.可以在Activity运行时将Fragmen

Android官方文档之App Components(Activities)

Activity是Android四大组件之首,本文将介绍Activity的含义.创建.启动.销毁.生命周期 等. 如需访问官方原文,您可以点击这个链接:<Activities> Activities Activity是一个类,它是Android呈现界面的载体,用于与用户操作交互,如拨号.照相.发送邮件.展示地图 等.每个Activity都承载了一个Window,这个Window用来绘制UI(User Interface).一般情况下,该Window铺满(fill)整个屏幕:有时候,它也可以悬浮

Android Studio中的App Components重要点记述

阅读英文文档而理解的file:///E:/Android2016/sdk/docs/guide/components/fundamentals.html#Components App Compnents 每个component都是系统可以进入你的app的一种方式,但是不是所有的component都是对于user而言的真实的entry points. 共有四种app components: Activity: 每一个activity代表用户界面的一个单独的屏幕,这些activity是相互独立的 S

Android官方文档之App Components(Intents and Intent Filters)

Android应用框架鼓励开发者在开发应用时重用组件,本文将阐述如何用组件构建应用程序以及如何用intent将组件联系起来. 如需阅读官方原文,请您点击这个链接: <App Components>. 您还可以参考这些博文: <Using DialogFragments> <Fragments For All> <Multithreading For Performance> 以及这些Training: <Managing the Activity Li

5、二、App Components(应用程序组件):0、概述

二.App Components(应用程序组件) 0.概述 ? App Components Android's application framework lets you create rich and innovative apps using a set of reusable components. This section explains how you can build the components that define the building blocks of your

Android官方文档之App Components(Common Intents)

Intent的真正强大之处在于它的隐式Intent,隐式Intent需要配合Intent-filters使用,有关这方面的概念和知识,您可以参考我翻译的官方文档:<Android官方文档之App Components(Intents and Intent Filters)>. 隐式Intent足够强大,以至于系统提供了大量的Intent方便开发者启动系统应用程序,本文将介绍一些常用的隐式Intent.以及如何自定义intent-filters以匹配隐式intent. 如需阅读官方原文,您可以点

Android官方文档之App Components(Common Intents)(转载)

原文地址:http://blog.csdn.net/vanpersie_9987/article/details/51244558#rd Intent的真正强大之处在于它的隐式Intent,隐式Intent需要配合Intent-filters使用,有关这方面的概念和知识,您可以参考我翻译的官方文档:<Android官方文档之App Components(Intents and Intent Filters)>. 隐式Intent足够强大,以至于系统提供了大量的Intent方便开发者启动系统应用

Android官方文档之App Components(Loaders)

Loaders在Android 3.0中引入.在Activity和Fragment中,使用Loaders可以方便地加载数据.有关Activity和Fragment的介绍,您可以参考我翻译的官方文档: <Activities> <Fragments> 本文将介绍Loaders API.Loaders的启动.重启.Loaders管理器 等内容,如需访问Loaders的官方原文,您可以点击这个链接:<Loaders>. Loaders Loaders具有如下特性: 它适用于任

Android官方文档之App Components(Tasks and Back Stack)

一个应用中包含了多个Activity实例,每个Activity都有各自的action,每个Activity也可以启动其他Activity,如一个Email应用程序应包含一个显示Email信息列表的Activity.当用户点击列表中的某一项时,显示详细内容的Activity将被启动. 本文将介绍Activity的栈和后退栈(Tasks and Back Stack)的相关知识,您需访问官方原文,您可以点击这个链接:<Tasks and Back Stack>. 一个Activity甚至可以启动其

6、二、App Components(应用程序组件):1、Intents and Intent Filters(意图和意图过滤器)

1.