一聚教程网:一个值得你收藏的教程网站

热门教程

Android开发教程之Fragment定义、创建与使用方法详解【包含Activity通讯,事务执行等】

时间:2022-06-25 23:11:49 编辑:袖梨 来源:一聚教程网

本文实例讲述了Android开发教程之Fragment定义、创建与使用方法。分享给大家供大家参考,具体如下:

概述

Fragment是activity的界面中的一部分或一种行为。你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment。你可以把Fragment认为模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除。

Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,它拥有的所有的Fragment们都暂停了,当activity销毁时,它拥有的所有Fragment们都被销毁。然而,当activity运行时(在onResume()之后,onPause()之前),你可以单独地操作每个Fragment,比如添加或删除它们。当你在执行上述针对Fragment的事务时,你可以将事务添加到一个棧中,这个栈被activity管理,栈中的每一条都是一个Fragment的一次事务。有了这个栈,就可以反向执行Fragment的事务,这样就可以在Fragment级支持“返回”键(向后导航)。

当向activity中添加一个Fragment时,它须置于ViewGroup控件中,并且需定义Fragment自己的界面。你可以在layoutxml文件中声明Fragment,元素为:;也可以在代码中创建Fragment,然后把它加入到ViewGroup控件中。然而,Fragment不一定非要放在activity的界面中,它可以隐藏在后台为actvitiy工作。

接下来讲如何使用fragment,包括fragment在加入activity的后退棧中时如何保持自己的状态,如何与activity以及其它fragment们共享事件,如何显示在activity的动作栏,等等。

Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用。平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们。Fragment使你在做那样的设计时,不需应付view树中复杂的变化。通过把activity的layout分成fragment,你可以在activity运行时改变它的样子,并且可以在activity的后退栈中保存这些改变。

例如:写一个读新闻的程序,可以用一个fragment显示标题列表,另一个fragment显示选中标题的内容,这两个fragment都在一个activity上,并排显示。那么这两个fragment都有自己的生命周期并响应自己感兴趣的事件。于是,不需再像手机上那样用一个activity显示标题列表,用另一个activity显示新闻内容;现在可以把两者放在一个activity上同时显示出来。

Fragment必须被写成可重用的模块。因为fragment有自己的layout,自己进行事件响应,拥有自己的生命周期和行为,所以你可以在多个activity中包含同一个Fragment的不同实例。这对于让你的界面在不同的屏幕尺寸下都能给用户完美的体验尤其重要。比如你可以在程序运行于大屏幕中时启动包含很多fragment的activity,而在运行于小屏幕时启动一个包含少量fragment的activity。

举个例子--还是刚才那个读新闻的程序-当你检测到程序运行于大屏幕时,启动activityA,你将标题列表和新闻内容这两个fragment都放在activityA中;当检测到程序运行于小屏幕时,还是启动activityA,但此时A中只有标题列表fragment,当选中一个标题时,activityA启动activityB,B中含有新闻内容fragment。

Fragments的生命周期

    每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件。 对应生命周期可参考下图:

创建片元(Creating a Fragment)

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.

要创建一个fragment,必须创建一个fragment的子类(或是继承自它的子类)。fragment类的代码看起来很像activity。它与activity一样都有回调函数,例如onCreate()onStart()onPause(),和onStop()。事实上,如果你正在将一个现成的Android应用转而使用Fragment来实现,可以简单的将代码从activity的回调函数移植到各自的fragment回调函数中。

Usually, you should implement at least the following lifecycle methods:
一般情况下,你至少需要实现以下几个生命周期方法:
onCreate()
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
在创建fragment时系统会调用此方法。在实现代码中,你可以初始化想要在fragment中保持的那些必要组件(这里的组件是指除了view之外的东西,比如需要进行界面展示的关键数据),当fragment处于暂停或者停止状态之后可重新启用它们。

onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
在第一次为fragment绘制用户界面时系统会调用此方法。为fragment绘制用户界面,这个函数必须要返回所绘出的fragment的根View。如果fragment没有用户界面可以返回空。

onPause()
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
系统回调用该函数作为用户离开fragment的第一个预兆(尽管这并不总意味着fragment被销毁)。在当前用户会话结束之前,通常要在这里提交任何应该持久化的变化(因为用户可能不再返回)。

Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
大部分应用程序都应该至少为每个fragment实现这三个方法,但是还有许多其他用以操纵fragment生命周期中各个阶段的回调函数。所有生命周期中的回调函数在操纵fragment生命周期一节中稍后再做讨论。

There are also a few subclasses that you might want to extend, instead of the base Fragment class:
除了基类fragment,这里还有几个你可能会继承的子类:

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.
显示一个浮动的对话框。使用这个类创建对话框是使用Activity类对话框工具方法之外的另一个不错的选择,因为你可以把fragment对话框并入到由activity管理的fragments后台栈中,允许用户返回到一个已经摒弃的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.
显示一个由适配器管理的条目列表(例如SimpleCursorAdapter),类似于ListActivity。并且提供了许多管理列表视图的函数,例如处理点击事件的onListItemClick()回调函数。

PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a "settings" activity for your application.
显示一个Preference对象的体系结构列表,类似于preferenceActivity。这在为应用程序创建“设置”activity时是很实用的。

实现Fragment的界面

为fragment添加用户界面:

Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中。 一个
要为fragment提供layout,你必须实现onCreateView()回调方法,然后在这个方法中返回一个View对象,这个对象是fragment的layout的根。

注:如果你的fragment是从ListFragment中派生的,就不需要实现onCreateView()方法了,因为默认的实现已经为你返回了ListView控件对象。

要从onCreateView()方法中返回layout对象,你可以从layoutxml中生成layout对象。为了帮助你这样做,onCreateView()提供了一个LayoutInflater对象。

举例:以下代码展示了一个Fragment的子类如何从layoutxml文件example_fragment.xml中生成对象。

publicstaticclassExampleFragmentextendsFragment{
  @Override
 publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
    //Inflate the layout for this fragment
    returninflater.inflate(R.layout.example_fragment,container,false);
  }
}

onCreateView()参数中的container是存放fragment的layout的ViewGroup对象。savedInstanceState参数是一个Bundle,跟activityonCreate()中Bundle差不多,用于状态恢复。但是fragment的onCreate()中也有Bundle参数,所以此处的Bundle中存放的数据与onCreate()中存放的数据还是不同的。至于详细信息,请参考“操控fragment的生命周期”一节。

Inflate()方法有三个参数:

1 layout的资源ID。

2 存放fragment的layout的ViewGroup。

3 布尔型数据表示是否在创建fragment的layout期间,把layout附加到container上(在这个例子中,因为系统已经把layout插入到container中了,所以值为false,如果为true会导至在最终的layout中创建多余的ViewGroup(这句我看不明白,但我翻译的应该没错))。

现在你看到如何为fragment创建layout了,下面讲述如何把它添加到activity中。

把fragment添加到activity

一般情况下,fragment把它的layout作为activitiy的loyout的一部分合并到activity中,有两种方法将一个fragment添加到activity中:

方法一:在activity的layoutxml文件中声明fragment

如下代码,一个activity中包含两个fragment:



  
  


中声明一个fragment。
当系统创建上例中的layout时,它实例化每一个fragment,然后调用它们的onCreateView()方法,以获取每个fragment的layout。系统把fragment返回的view对象插入到元素的位置,直接代替元素。

注:每个fragment都需要提供一个ID,系统在activity重新创建时用它来恢复fragment们,你也可以用它来操作fragment进行其它的事物,比如删除它。有三种方法给fragment提供ID:

1 为android:id属性赋一个特定的标识符。
2 为android:tag属性赋一个标记名称。
3如果你没有使用上述任何一种方法,系统将使用fragment的容器的ID。

方法二:在代码中添加fragment到一个ViewGroup

这种方法可以在运行时,把fragment添加到activity的layout中。你只需指定一个要包含fragment的ViewGroup。

为了完成fragment的事务(比如添加,删除,替换等),你必须使用FragmentTransaction的方法。你可以从activity获取到FragmentTransaction,如下:

FragmentManagerfragmentManager =getFragmentManager()
FragmentTransactionfragmentTransaction =fragmentManager.beginTransaction();

然后你可以用add()方法添加一个fragment,它有参数用于指定容纳fragment的ViewGroup。如下:

ExampleFragmentfragment =newExampleFragment();
fragmentTransaction.add(R.id.fragment_container,fragment);
fragmentTransaction.commit();

Add()的第一个参数是容器ViewGroup,第二个是要添加的fragment。一旦你通过FragmentTransaction对fragment做出了改变,你必须调用方法commit()提交这些改变。

不仅在无界面的fragment中,在有界面的fragment中也可以使用tag来作为为一标志,这样在需要获取fragment对象时,要调用findFragmentTag()

添加一个没有界面的fragment

上面演示了如何添加fragment来提供界面,然而,你也可以使用fragment为activity提供后台的行为而不用显示fragment的界面。

要添加一个没有界面的fragment,需在activity中调用方法add(Fragment,String)(它支持用一个唯一的字符串做为fragment的”tag”,而不是viewID)。这样添加的fragment由于没有界面,所以你在实现它时不需调用实现onCreateView()方法。

使用tag字符串来标识一个fragment并不是只能用于没有界面的fragment上,你也可以把它用于有界面的fragment上,但是,如果一个fragment没有界面,tag字符串将成为它唯一的选择。获取以tag标识的fragment,需使用方法findFragmentByTab()

管理Fragment

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()

你可以用FragmentManager来做以上事情:

1 使用方法findFragmentById()findFragmentByTag(),获取activity中已存在的fragment们。
2 使用方法popBackStack()从activity的后退栈中弹出fragment们(这可以模拟后退键引发的动作)。
3 用方法addOnBackStackChangedListerner()注册一个侦听器以监视后退栈的变化。

你还可以使用FragmentManager打开一个FragmentTransaction来执行fragment的事务,比如添加或删除fragment。

执行Fragment的事务

在activity中使用fragment的一个伟大的好处是能跟据用户的输入对fragment进行添加、删除、替换以及执行其它动作的能力。你提交的一组fragment的变化叫做一个事务。事务通过FragmentTransaction来执行。你还可以把每个事务保存在activity的后退栈中,这样就可以让用户在fragment变化之间导航(跟在activity之间导航一样)。

你可以通过FragmentManager来取得FragmentTransaction的实例,如下:

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

一个事务是在同一时刻执行的一组动作(很像数据库中的事务)。你可以用add(),remove(),replace()等方法构成事务,最后使用commit()方法提交事务。

在调用commit()之前,你可以用addToBackStack()把事务添加到一个后退栈中,这个后退栈属于所在的activity。有了它,就可以在用户按下返回键时,返回到fragment们执行事务之前的状态。

如下例:演示了如何用一个fragment代替另一个fragment,同时在后退栈中保存被代替的fragment的状态。

//Create new fragment and transaction
Fragment newFragment = newExampleFragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
//Replace whatever is in the fragment_container view with thisfragment,
//and add the transaction to the backstack
transaction.replace(R.id.fragment_container,newFragment);
transaction.addToBackStack(null);
//Commit the transaction
transaction.commit();

解释:newFragment代替了控件IDR.id.fragment_container所指向的ViewGroup中所含的任何fragment。然后调用addToBackStack(),此时被代替的fragment就被放入后退栈中,于是当用户按下返回键时,事务发生回溯,原先的fragment又回来了。

如果你向事务添加了多个动作,比如多次调用了add(),remove()等之后又调用了addToBackStack()方法,那么所有的在commit()之前调用的方法都被作为一个事务。当用户按返回键时,所有的动作都被反向执行(事务回溯)。

事务中动作的执行顺序可随意,但要注意以下两点:

1. 你必须最后调用commit()。

2. 如果你添加了多个fragment,那么它们的显示顺序跟添加顺序一至(后显示的覆盖前面的)。

如果你在执行的事务中有删除fragment的动作,而且没有调用addToBackStack(),那么当事务提交时,那些被删除的fragment就被销毁了。反之,那些fragment就不会被销毁,而是处于停止状态。当用户返回时,它们会被恢复。

密技:对于fragment事务,你可以应用动画。在commit()之前调用setTransition()就行。――一般银我不告诉他哦。

但是,调用commit()后,事务并不会马上执行。它会在activity的UI线程(其实就是主线程)中等待直到线程能执行的时候才执行(废话)。如果必要,你可以在UI线程中调用executePendingTransactions()方法来立即执行事务。但一般不需这样做,除非有其它线程在等待事务的执行。

警告:你只能在activity处于可保存状态的状态时,比如running中,onPause()方法和onStop()方法中提交事务,否则会引发异常。这是因为fragment的状态会丢失。如果要在可能丢失状态的情况下提交事务,请使用commitAllowingStateLoss()。

Fragment与Activity通讯

尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。

Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:

ViewlistView =getActivity().findViewById(R.id.list);

同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。例如:

ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment)

activity响应fragment的事件

有时,你可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。

例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。如下例,OnArticleSelectedListener接口在fragmentA中定义:

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

然后activity实现接口OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,如下所示:

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");
   }
 }
 ...

如果activity没有实现那个接口,fragment抛出ClassCastException异常。如果成功了,mListener成员变量保存OnArticleSelectedListener的实例。于是fragmentA就可以调用mListener的方法来与activity共享事件。例如,如果fragmentA是一个ListFragment,每次选中列表的一项时,就会调用fragmentA的onListItemClick()方法,在这个方法中调用onArticleSelected()来与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);
 }
 ...

onListItemClick()传入的参数id是列表的被选中的行ID,另一个fragment用这个ID来从程序的ContentProvider中取得标题的内容。

Fragement应用示例

把条目添加到动作栏

你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptionManu()来完成。

你从fragment添加到菜单的任何条目,都会出现在现有菜单项之后。Fragment之后可以通过方法onOptionsItemSelected()来响应自己的菜单项被选择的事件。

你也可以在fragemnt中注册一个view来提供快捷菜单(上下文菜单)。当用户要打开快捷菜单时,fragment的onCreateContextMenu()方法会被调用。当用户选择其中一项时,fragemnt的onContextItemSelected()方法会被调用。

注:尽管你的fragment可以分别收到它所添加的菜单项的选中事件,但是activity才是第一个接收这些事件的家伙,只有当activity对某个事件置之不理时,fragment才能接收到这个事件,对于菜单和快捷菜单都是这样。

下例中实验了之前所讲的所有内容。此例有一个activity,其含有两个fragment。一个显示莎士比亚剧的播放曲目,另一个显示选中曲目的摘要。此例还演示了如何跟据屏幕大小配置fragment。

MainActivity:

@Override
protectedvoid onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.fragment_layout);
}

Layout.xml:


  
  


系统在activity加载此layout时初始化TitlesFragment(用于显示标题列表),TitlesFragment的右边是一个FrameLayout,用于存放显示摘要的fragment,但是现在它还是空的,fragment只有当用户选择了一项标题后,摘要fragment才会被放到FrameLayout中。

然而,并不是所有的屏幕都有足够的宽度来容纳标题列表和摘要。所以,上述layout只用于横屏,现把它存放于ret/layout-land/fragment_layout.xml。

之外,当用于竖屏时,系统使用下面的layout,它存放于ret/layout/fragment_layout.xml:


  


这个layout只包含TitlesFragment。这表示当使用竖屏时,只显示标题列表。当用户选中一项时,程序会启动一个新的activity去显示摘要,而不是加载第二个fragment。

下一步,你会看到Fragment类的实现。第一个是TitlesFragment,它从ListFragment派生,大部分列表的功能由ListFragment提供。

当用户选择一个Title时,代码需要做出两种行为,一种是在同一个activity中显示创建并显示摘要fragment,另一种是启动一个新的activity。

public static class TitlesFragment extends ListFragment {
  boolean mDualPane;
  int mCurCheckPosition = 0;
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Populate list with our static array of titles.
    setListAdapter(new ArrayAdapter(getActivity(),
        android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
    // Check to see if we have a frame in which to embed the details
    // fragment directly in the containing UI.
    View detailsFrame = getActivity().findViewById(R.id.details);
    mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
    if (savedInstanceState != null) {
      // Restore last state for checked position.
      mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }
    if (mDualPane) {
      // In dual-pane mode, the list view highlights the selected item.
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      // Make sure our UI is in the correct state.
      showDetails(mCurCheckPosition);
    }
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
  }
  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    showDetails(position);
  }
  /**
   * Helper function to show the details of a selected item, either by
   * displaying a fragment in-place in the current UI, or starting a
   * whole new activity in which it is displayed.
   */
  void showDetails(int index) {
    mCurCheckPosition = index;
    if (mDualPane) {
      // We can display everything in-place with fragments, so update
      // the list to highlight the selected item and show the data.
      getListView().setItemChecked(index, true);
      // Check what fragment is currently shown, replace if needed.
      DetailsFragment details = (DetailsFragment)
          getFragmentManager().findFragmentById(R.id.details);
      if (details == null || details.getShownIndex() != index) {
        // Make new fragment to show this selection.
        details = DetailsFragment.newInstance(index);
        // Execute a transaction, replacing any existing fragment
        // with this one inside the frame.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.details, details);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
      }
    } else {
      // Otherwise we need to launch a new activity to display
      // the dialog fragment with selected text.
      Intent intent = new Intent();
      intent.setClass(getActivity(), DetailsActivity.class);
      intent.putExtra("index", index);
      startActivity(intent);
    }
  }
}

第二个fragment,DetailsFragment显示被选择的Title的摘要:

public static class DetailsFragment extends Fragment {
  /**
   * Create a new instance of DetailsFragment, initialized to
   * show the text at 'index'.
   */
  public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();
    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);
    return f;
  }
  public int getShownIndex() {
    return getArguments().getInt("index", 0);
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    if (container == null) {
      // We have different layouts, and in one of them this
      // fragment's containing frame doesn't exist. The fragment
      // may still be created from its saved state, but there is
      // no reason to try to create its view hierarchy because it
      // won't be displayed. Note this is not needed -- we could
      // just run the code below, where we would create and return
      // the view hierarchy; it would just never be used.
      return null;
    }
    ScrollView scroller = new ScrollView(getActivity());
    TextView text = new TextView(getActivity());
    int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        4, getActivity().getResources().getDisplayMetrics());
    text.setPadding(padding, padding, padding, padding);
    scroller.addView(text);
    text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
    return scroller;
  }
}

如果当前的layout没有R.id.detailsView(它被用于DetailsFragment的容器),那么程序就启动DetailsActivity来显示摘要。

下面是DetailsActivity,它只是简单地嵌入DetailsFragment来显示摘要。

public static class DetailsActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getResources().getConfiguration().orientation
        == Configuration.ORIENTATION_LANDSCAPE) {
      // If the screen is now in landscape mode, we can show the
      // dialog in-line with the list so we don't need this activity.
      finish();
      return;
    }
    if (savedInstanceState == null) {
      // During initial setup, plug in the details fragment.
      DetailsFragment details = new DetailsFragment();
      details.setArguments(getIntent().getExtras());
      getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
    }
  }
}

注意这个activity在检测到是竖屏时会结束自己,于是主activity会接管它并显示出TitlesFragment和DetailsFragment。这可以在用户在竖屏时显示在TitleFragment,但用户旋转了屏幕,使显示变成了横屏。

热门栏目