Android Fragment 真正的完全解析

activity的切换问题(activity与栈) - look85的专栏 - 博客频道 - CSDN.NET

一、Activity和Task(栈)的关系

   Task就像一个容器,而Activity就相当与填充这个容器的东西,第一个东西(Activity)则会处于最下面,最后添加的东西 (Activity)则会在最上面。从Task中取出东西(Activity)是从最顶端取出,也就是说最先取出的是最后添加的东西 (Activity),以此类推,最后取出的是第一次添加的Activity,而Activity在Task中的顺序是可以控制的,在Activity跳 转时用到Intent Flag可以设置新建activity的创建方式;

二、具体Intent用法如下:

  //默认的跳转类型,会重新创建一个新的Activity

  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  //如果activity在task存在,拿到最顶端,不会启动新的Activity

  intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

  //如果activity在task存在,将Activity之上的所有Activity结束掉

  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  //如果Activity已经运行到了Task,再次跳转不会启动新的Activity

  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

《以下为转载内容》

什么是Affinity

在 某些情况下,Android需要知个Activity道一属于哪个Task,即使它没有被启动到一个具体的Task里。这是通过任务共用性 (Affinities)完成的。任务共用性(Affinities)为这个运行一个或多个Activity的Task提供了一个独特的静态名称,默认的 一个活动的任务共用性(Affinity)是实现了该Activity的.apk包的名字。

当开始一个没有 Intent.FLAG_ACTIVITY_NEW_TASK标志的Activity时,任务共用性affinities不会影响将会运行该新活动的 Task:它总是运行在启动它的Task里。但是,如果使用了NEW_TASK标志,那么共用性(affinity)将被用来判断是否已经存在一个有相同 共用性(affinity)的Task。如果是这样,这项Task将被切换到前面而新的Activity会启动于这个Task的顶层。

这种特性在您必须使用NEW_TASK标志的情况下最有用,尤其是从状态栏通知或桌面快捷方式启动活动时。结果是,当用户用这种方式启动您的应用程序时,它的当前Task将被切换到前台,而且想要查看的Activity被放在最上面。

你可以在程序清单(Manifest)文件的应用程序application标签中为.apk包中所有的活动分配你自己的任务共用性Affinites,或者在活动标记中为各个活动进行分配。

一些说明其如何使用的例子如下:

  • 如 果您的.apk包含多个用户可以启动的高层应用程序,那么您可能需要对用户看到的每个Activity(活动)指定不同的affinities。一个不错 的命名惯例是以附加一个以冒号分隔的字符串来扩展您的.apk包名。例如,“ com.android.contacts ”.apk可以有affinities:“com.android.contacts:Dialer”和“ com.android.contacts:ContactsList”。
  • 如果您正在替换一个通知,快捷方式,或其他可以从外部发起的 应用程序的“内部”活动,你可能需要明确设定您替代活动的taskAffinity和您准备替代的应用程序一样。例如,如果您想替换contacts详细 信息视图(用户可以创建并调用快捷方式),你得把taskAffinity设置成“com.android.contacts”。

Activity的加载模式受启动Activity的Intent对象中设置的Flag和manifest文件中Activity的<activity>元素的特性值交互控制。

跟 Task 有关的 manifest文件中Activity的特性值介绍

android:allowTaskReparenting 

    用来标记Activity能否从启动的Task移动到有着affinity的Task(当这个Task进入到前台时)

   “true”,表示能移动,“false”,表示它必须呆在启动时呆在的那个Task里。

    如果这个特性没有被设定,设定到<application>元素上的allowTaskReparenting特性的值会应用到Activity上。默认值为“false”。

    一般来说,当Activity启动后,它就与启动它的Task关联,并且在那里耗尽它的整个生命周期。当当前的Task不再显示时,你可以使用这个特性来 强制Activity移动到有着affinity的Task中。典型用法是:把一个应用程序的Activity移到另一个应用程序的主Task中。 

    例如,如果 email中包含一个web页的链接,点击它就会启动一个Activity来显示这个页面。这个Activity是由Browser应用程序定义的,但 是,现在它作为email Task的一部分。如果它重新宿主到Browser Task里,当Browser下一次进入到前台时,它就能被看见,并且,当email Task再次进入前台时,就看不到它了。 

    Actvity的affinity是由taskAffinity特性定义的。Task的affinity是通过读取根Activity的affinity 决定。因此,根Activity总是位于相同affinity的Task里。由于启动模式为“singleTask”和“singleInstance” 的Activity只能位于Task的底部,因此,重新宿主只能限于“standard”和“singleTop”模式。

android:alwaysRetainTaskState 

    用来标记Activity所在的Task的状态是否总是由系统来保持。

    “true”,表示总是;“false”,表示在某种情形下允许系统恢复Task到它的初始化状态。默认值是“false”。

    这个特性只针对Task的根Activity有意义;对其它Activity来说,忽略之。 

    一般来说,特定的情形如当用户从主画面重新选择这个Task时,系统会对这个Task进行清理(从stack中删除位于根Activity之上的所有Activivity)。典型的情况,当用户有一段时间没有访问这个Task时也会这么做,例如30分钟。 

    然而,当这个特性设为“true”时,用户总是能回到这个Task的最新状态,无论他们是如何启动的。这非常有用,例如,像Browser应用程序,这里有很多的状态(例如多个打开的Tab),用户不想丢失这些状态。

android:clearTaskOnLaunch 

    用来标记是否从Task中清除所有的Activity,除了根Activity外(每当从主画面重新启动时)

   “true”,表示总是清除至它的根Activity,“false”表示不。默认值是“false”。

    这个特性只对启动一个新的Task的Activity(根Activity)有意义;对Task中其它的Activity忽略。 

    当这个值为“true”,每次用户重新启动这个Task时,都会进入到它的根Activity中,不管这个Task最后在做些什么,也不管用户是使用 BACK还是HOME离开的。当这个值为“false”时,可能会在一些情形下(参考alwaysRetainTaskState特性)清除Task的 Activity,但不总是。 

    假设,某人从主画面启动了Activity P,并从那里迁移至Activity Q。接下来用户按下HOME,然后返回Activity P。一般,用户可能见到的是Activity Q,因为它是P的Task中最后工作的内容。然而,如果P设定这个特性为“true”,当用户按下HOME并使这个Task再次进入前台时,其上的所有的 Activity(在这里是Q)都将被清除。因此,当返回到这个Task时,用户只能看到P。 

    如果这个特性和allowTaskReparenting都设定为“true”,那些能重新宿主的Activity会移动到共享affinity的Task中;剩下的Activity都将被抛弃,如上所述。

android:finishOnTaskLaunch 

    用来标记当用户再次启动它的Task(在主画面选择这个Task)时已经存在的Activity实例是否要关闭(结束)

   “true”,表示应该关闭,“false”表示不关闭。默认值是“false”。 

    如果这个特性和allowTaskReparenting都设定为“true”,这个特性胜出。Activity的affinity忽略。这个Activity不会重新宿主,但是会销毁。

android:launchMode 

    用于指示Activity如何启动。这里有四种模式,与Intent对象中的Activity Flags(FLAG_ACTIVITY_*变量)共同作用,来决定Activity如何启动来处理Intent。它们是:

    "standard" 

    "singleTop" 

    "singleTask" 

    "singleInstance"

    默认模式是“standard”。 

android:noHistory 

    用于标记当用户从Activity上离开并且它在屏幕上不再可见时Activity是否从Activity stack中清除并结束(调用finish()方法)——“true”,表示它应该关闭,“false”,表示不需要。默认值是“false”。 

    “true”值意味着Activity不会留下历史痕迹。因为它不会在Activity stack的Task中保留,因此,用户不能返回它。

    比如启用界面的就可以借用这个。

android:taskAffinity 

   这就是本文所描述的任务共用性。

   Activity为Task拥有的一个affinity。拥有相同的affinity的Activity理论上属于相同的Task(在用户的角度是相同的“应用程序”)。Task的affinity是由它的根Activity决定的。 

   affinity决定两件事情——Activity重新宿主的Task(参考allowTaskReparenting特性)和使用FLAG_ACTIVITY_NEW_TASK标志启动的Activity宿主的Task。 

    默认情况,一个应用程序中的所有Activity都拥有相同的affinity。捏可以设定这个特性来重组它们,甚至可以把不同应用程序中定义的Activity放置到相同的Task中。为了明确Activity不宿主特定的Task,设定该特性为空的字符串。 

    如果这个特性没有设置,Activity将从应用程序的设定那里继承下来(参考<application>元素的taskAffinity特 性)。应用程序默认的affinity的名字是<manifest>元素中设定的package名。

跟 Task 有关的 Intent对象中设置的Flag

FLAG_ACTIVITY_BROUGHT_TO_FRONT 

    这个标志一般不是由程序代码设置的,如在launchMode中设置singleTask模式时系统帮你设定。

FLAG_ACTIVITY_CLEAR_TOP 

    如果设置,并且这个Activity已经在当前的Task中运行,因此,不再是重新启动一个这个Activity的实例,而是在这个Activity上方 的所有Activity都将关闭,然后这个Intent会作为一个新的Intent投递到老的Activity(现在位于顶端)中。 

    例如,假设一个Task中包含这些Activity:A,B,C,D。如果D调用了startActivity(),并且包含一个指向Activity B的Intent,那么,C和D都将结束,然后B接收到这个Intent,因此,目前stack的状况是:A,B。 

    上例中正在运行的Activity B既可以在onNewIntent()中接收到这个新的Intent,也可以把自己关闭然后重新启动来接收这个Intent。如果它的启动模式声明为 “multiple”(默认值),并且你没有在这个Intent中设置FLAG_ACTIVITY_SINGLE_TOP标志,那么它将关闭然后重新创 建;对于其它的启动模式,或者在这个Intent中设置FLAG_ACTIVITY_SINGLE_TOP标志,都将把这个Intent投递到当前这个实 例的onNewIntent()中。 

    这个启动模式还可以与FLAG_ACTIVITY_NEW_TASK结合起来使用:用于启动一个Task中的根Activity,它会把那个Task中任 何运行的实例带入前台,然后清除它直到根Activity。这非常有用,例如,当从Notification Manager处启动一个Activity。

FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 

    如果设置,这将在Task的Activity stack中设置一个还原点,当Task恢复时,需要清理Activity。也就是说,下一次Task带着 FLAG_ACTIVITY_RESET_TASK_IF_NEEDED标记进入前台时(典型的操作是用户在主画面重启它),这个Activity和它之 上的都将关闭,以至于用户不能再返回到它们,但是可以回到之前的Activity。 

    这在你的程序有分割点的时候很有用。例如,一个e-mail应用程序可能有一个操作是查看一个附件,需要启动图片浏览Activity来显示。这个 Activity应该作为e-mail应用程序Task的一部分,因为这是用户在这个Task中触发的操作。然而,当用户离开这个Task,然后从主画面 选择e-mail app,我们可能希望回到查看的会话中,但不是查看图片附件,因为这让人困惑。通过在启动图片浏览时设定这个标志,浏览及其它启动的Activity在下 次用户返回到mail程序时都将全部清除。

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 

    如果设置,新的Activity不会在最近启动的Activity的列表中保存。

FLAG_ACTIVITY_FORWARD_RESULT 

    如果设置,并且这个Intent用于从一个存在的Activity启动一个新的Activity,那么,这个作为答复目标的Activity将会传到这个 新的Activity中。这种方式下,新的Activity可以调用setResult(int),并且这个结果值将发送给那个作为答复目标的 Activity。

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 

    这个标志一般不由应用程序代码设置,如果这个Activity是从历史记录里启动的(常按HOME键),那么,系统会帮你设定。

FLAG_ACTIVITY_MULTIPLE_TASK 

    不要使用这个标志,除非你自己实现了应用程序启动器。与FLAG_ACTIVITY_NEW_TASK结合起来使用,可以禁用把已存的Task送入前台的 行为。当设置时,新的Task总是会启动来处理Intent,而不管这是是否已经有一个Task可以处理相同的事情。 

    由于默认的系统不包含图形Task管理功能,因此,你不应该使用这个标志,除非你提供给用户一种方式可以返回到已经启动的Task。 

    如果FLAG_ACTIVITY_NEW_TASK标志没有设置,这个标志被忽略。

FLAG_ACTIVITY_NEW_TASK 

    如果设置,这个Activity会成为历史stack中一个新Task的开始。一个Task(从启动它的Activity到下一个Task中的 Activity)定义了用户可以迁移的Activity原子组。Task可以移动到前台和后台;在某个特定Task中的所有Activity总是保持相 同的次序。 

    这个标志一般用于呈现“启动”类型的行为:它们提供用户一系列可以单独完成的事情,与启动它们的Activity完全无关。 

    使用这个标志,如果正在启动的Activity的Task已经在运行的话,那么,新的Activity将不会启动;代替的,当前Task会简单的移入前台。参考FLAG_ACTIVITY_MULTIPLE_TASK标志,可以禁用这一行为。 

    这个标志不能用于调用方对已经启动的Activity请求结果。

FLAG_ACTIVITY_NO_ANIMATION 

    如果在Intent中设置,并传递给Context.startActivity()的话,这个标志将阻止系统进入下一个Activity时应用 Acitivity迁移动画。这并不意味着动画将永不运行——如果另一个Activity在启动显示之前,没有指定这个标志,那么,动画将被应用。这个标 志可以很好的用于执行一连串的操作,而动画被看作是更高一级的事件的驱动。

FLAG_ACTIVITY_NO_HISTORY 

    如果设置,新的Activity将不再历史stack中保留。用户一离开它,这个Activity就关闭了。这也可以通过设置noHistory特性。

FLAG_ACTIVITY_NO_USER_ACTION 

    如果设置,作为新启动的Activity进入前台时,这个标志将在Activity暂停之前阻止从最前方的Activity回调的onUserLeaveHint()。 

    典型的,一个Activity可以依赖这个回调指明显式的用户动作引起的Activity移出后台。这个回调在Activity的生命周期中标记一个合适的点,并关闭一些Notification。 

    如果一个Activity通过非用户驱动的事件,如来电或闹钟,启动的,这个标志也应该传递给Context.startActivity,保证暂停的Activity不认为用户已经知晓其Notification。

FLAG_ACTIVITY_PREVIOUS_IS_TOP 

    If set and this intent is being used to launch a new activity from an existing one, the current activity will not be counted as the top activity for deciding whether the new intent should be delivered to the top instead of starting a new one. The previous
activity will be used as the top, with the assumption being that the current activity will finish itself immediately.

FLAG_ACTIVITY_REORDER_TO_FRONT 

    如果在Intent中设置,并传递给Context.startActivity(),这个标志将引发已经运行的Activity移动到历史stack的顶端。 

    例如,假设一个Task由四个Activity组成:A,B,C,D。如果D调用startActivity()来启动Activity B,那么,B会移动到历史stack的顶端,现在的次序变成A,C,D,B。如果FLAG_ACTIVITY_CLEAR_TOP标志也设置的话,那么这 个标志将被忽略。

FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

If set, and this activity is either being started in a new task or bringing to the top an existing task, then it will be launched as the front door of the task. This will result in the application of any affinities needed to have that task in the proper
state (either moving activities to or from it), or simply resetting that task to its initial state if needed.

FLAG_ACTIVITY_SINGLE_TOP 

    如果设置,当这个Activity位于历史stack的顶端运行时,不再启动一个新的。

Android Fragment 真正的完全解析(上) - Hongyang - 博客频道 - CSDN.NET

自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~

本 篇博客力求为大家说明Fragment如何产生,什么是Fragment,Fragment生命周期,如何静态和动态的使用 Fragment,Fragment回退栈,Fragment事务;以及Fragment的一些特殊用途,例如:没有布局的Fragment有何用 处?Fragment如何与Activity交互?Fragment如何创建对话框?Fragment如何与ActionBar集成等等。

1、Fragment的产生与介绍

Android 运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布 局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可 以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是 Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添 加、替换和移除某个Fragment。

2、Fragment的生命周期

Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:

可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,

3、静态的使用Fragment

嘿嘿,终于到使用的时刻了~~

这是使用Fragment最简单的一种方式,把Fragment当成普通的控件,直接写在Activity的布局文件中。步骤:

1、继承Fragment,重写onCreateView决定Fragemnt的布局

2、在Activity中声明此Fragment,就当和普通的View一样

下面展示一个例子(我使用2个Fragment作为Activity的布局,一个Fragment用于标题布局,一个Fragment用于内容布局):

TitleFragment的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="45dp"  
  5.     android:background="@drawable/title_bar" >  
  6.   
  7.     <ImageButton  
  8.         android:id="@+id/id_title_left_btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerVertical="true"  
  12.         android:layout_marginLeft="3dp"  
  13.         android:background="@drawable/showleft_selector" />  
  14.   
  15.     <TextView  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent"  
  18.         android:gravity="center"  
  19.         android:text="我不是微信"  
  20.         android:textColor="#fff"  
  21.         android:textSize="20sp"  
  22.         android:textStyle="bold" />  
  23.   
  24. </RelativeLayout>  

TitleFragment

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup;  
  9. import android.widget.ImageButton;  
  10. import android.widget.Toast;  
  11.   
  12. public class TitleFragment extends Fragment  
  13. {  
  14.   
  15.     private ImageButton mLeftMenu;  
  16.   
  17.     @Override  
  18.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  19.             Bundle savedInstanceState)  
  20.     {  
  21.         View view = inflater.inflate(R.layout.fragment_title, container, false);  
  22.         mLeftMenu = (ImageButton) view.findViewById(R.id.id_title_left_btn);  
  23.         mLeftMenu.setOnClickListener(new OnClickListener()  
  24.         {  
  25.             @Override  
  26.             public void onClick(View v)  
  27.             {  
  28.                 Toast.makeText(getActivity(),  
  29.                         "i am an ImageButton in TitleFragment ! ",  
  30.                         Toast.LENGTH_SHORT).show();  
  31.             }  
  32.         });  
  33.         return view;  
  34.     }  
  35. }  

同理还有ContentFragment的其布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:gravity="center"  
  11.         android:text="使用Fragment做主面板"  
  12.         android:textSize="20sp"  
  13.         android:textStyle="bold" />  
  14.   
  15. </LinearLayout>  
  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class ContentFragment extends Fragment  
  10. {  
  11.   
  12.     @Override  
  13.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  14.             Bundle savedInstanceState)  
  15.     {  
  16.         return inflater.inflate(R.layout.fragment_content, container, false);  
  17.     }  
  18.   
  19. }  

MainActivity

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6.   
  7. public class MainActivity extends Activity  
  8. {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState)  
  12.     {  
  13.         super.onCreate(savedInstanceState);  
  14.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  15.         setContentView(R.layout.activity_main);  
  16.     }  
  17.   
  18. }  

Activity的布局文件:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <fragment  
  7.         android:id="@+id/id_fragment_title"  
  8.         android:name="com.zhy.zhy_fragments.TitleFragment"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="45dp" />  
  11.   
  12.     <fragment  
  13.         android:layout_below="@id/id_fragment_title"  
  14.         android:id="@+id/id_fragment_content"  
  15.         android:name="com.zhy.zhy_fragments.ContentFragment"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent" />  
  18.   
  19. </RelativeLayout>  

是不是把Fragment当成普通的View一样声明在Activity的布局文件中,然后所有控件的事件处理等代码都由各自的Fragment去处理,瞬间觉得Activity好干净有木有~~代码的可读性、复用性以及可维护性是不是瞬间提升了~~~下面看下效果图:

4、动态的使用Fragment

上面已经演示了,最简单的使用Fragment的方式~下面介绍如何动态的添加、更新、以及删除Fragment

为了动态使用Fragment,我们修改一下Actvity的布局文件,中间使用一个FrameLayout,下面添加四个按钮~~~嘿嘿~~不是微信的按钮- -!

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <fragment  
  7.         android:id="@+id/id_fragment_title"  
  8.         android:name="com.zhy.zhy_fragments.TitleFragment"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="45dp" />  
  11.   
  12.     <include  
  13.         android:id="@+id/id_ly_bottombar"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="55dp"  
  16.         android:layout_alignParentBottom="true"  
  17.         layout="@layout/bottombar" />  
  18.   
  19.     <FrameLayout  
  20.         android:id="@+id/id_content"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="fill_parent"  
  23.         android:layout_above="@id/id_ly_bottombar"  
  24.         android:layout_below="@id/id_fragment_title" />  
  25.   
  26. </RelativeLayout>  

底部四个按钮的布局就不贴了,到时看效果图就明白了~~

下面主Activity

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.Window;  
  10. import android.widget.LinearLayout;  
  11.   
  12. public class MainActivity extends Activity implements OnClickListener  
  13. {  
  14.     private LinearLayout mTabWeixin;  
  15.     private LinearLayout mTabFriend;  
  16.   
  17.     private ContentFragment mWeixin;  
  18.     private FriendFragment mFriend;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState)  
  22.     {  
  23.         super.onCreate(savedInstanceState);  
  24.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  25.         setContentView(R.layout.activity_main);  
  26.   
  27.         // 初始化控件和声明事件  
  28.         mTabWeixin = (LinearLayout) findViewById(R.id.tab_bottom_weixin);  
  29.         mTabFriend = (LinearLayout) findViewById(R.id.tab_bottom_friend);  
  30.         mTabWeixin.setOnClickListener(this);  
  31.         mTabFriend.setOnClickListener(this);  
  32.   
  33.         // 设置默认的Fragment  
  34.         setDefaultFragment();  
  35.     }  
  36.   
  37.     private void setDefaultFragment()  
  38.     {  
  39.         FragmentManager fm = getFragmentManager();  
  40.         FragmentTransaction transaction = fm.beginTransaction();  
  41.         mWeixin = new ContentFragment();  
  42.         transaction.replace(R.id.id_content, mWeixin);  
  43.         transaction.commit();  
  44.     }  
  45.   
  46.     @Override  
  47.     public void onClick(View v)  
  48.     {  
  49.         FragmentManager fm = getFragmentManager();  
  50.         // 开启Fragment事务  
  51.         FragmentTransaction transaction = fm.beginTransaction();  
  52.   
  53.         switch (v.getId())  
  54.         {  
  55.         case R.id.tab_bottom_weixin:  
  56.             if (mWeixin == null)  
  57.             {  
  58.                 mWeixin = new ContentFragment();  
  59.             }  
  60.             // 使用当前Fragment的布局替代id_content的控件  
  61.             transaction.replace(R.id.id_content, mWeixin);  
  62.             break;  
  63.         case R.id.tab_bottom_friend:  
  64.             if (mFriend == null)  
  65.             {  
  66.                 mFriend = new FriendFragment();  
  67.             }  
  68.             transaction.replace(R.id.id_content, mFriend);  
  69.             break;  
  70.         }  
  71.         // transaction.addToBackStack();  
  72.         // 事务提交  
  73.         transaction.commit();  
  74.     }  
  75.   
  76. }  

可以看到我们使用FragmentManager对Fragment进行了动态的加载,这里使用的是replace方法~~下一节我会详细介绍FragmentManager的常用API。

注: 如果使用Android3.0以下的版本,需要引入v4的包,然后Activity继承FragmentActivity,然后通过 getSupportFragmentManager获得FragmentManager。不过还是建议版Menifest文件的uses-sdk的 minSdkVersion和targetSdkVersion都改为11以上,这样就不必引入v4包了。

代码中间还有两个Fragment的子类,ContentFragment上面已经见过,FriendFragment其实类似:

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class FriendFragment extends Fragment  
  10. {  
  11.   
  12.     @Override  
  13.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  14.             Bundle savedInstanceState)  
  15.     {  
  16.         return inflater.inflate(R.layout.fragment_friend, container, false);  
  17.     }  
  18.   
  19. }  

效果图:

可以看到很好的实现了效果,其实这个效果以前的博客中也出现过,在博客:Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager,有兴趣可以看看。ps:为了代码的简洁,就不添加按钮的点击变化什么的了,主要讲解功能了~~~

5、Fragment家族常用的API

Fragment常用的三个类:

android.app.Fragment 主要用于定义Fragment

android.app.FragmentManager 主要用于在Activity中操作Fragment

android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~

a、获取FragmentManage的方式:

getFragmentManager() // v4中,getSupportFragmentManager

b、主要的操作都是FragmentTransaction的方法

FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务

transaction.add() 

往Activity中添加一个Fragment

transaction.remove()

从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。

transaction.replace()

使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~

transaction.hide()

隐藏当前的Fragment,仅仅是设为不可见,并不会销毁

transaction.show()

显示之前隐藏的Fragment

detach()

将此Fragment从Activity中分离,会销毁其布局,但不会销毁该实例

attach()

将从Activity中分离的Fragment,重新关联到该Activity,重新创建其视图层次

transatcion.commit()//提交一个事务

注意:常用Fragment的哥们,可能会经常遇到这样Activity状态不一致:State loss这样的错误。主要是因为:commit方法一定要在Activity.onSaveInstance()之前调用。

上述,基本是操作Fragment的所有的方式了,在一个事务开启到提交可以进行多个的添加、移除、替换等操作。

值得注意的是:如果你喜欢使用Fragment,一定要清楚这些方法,哪个会销毁视图,哪个会销毁实例,哪个仅仅只是隐藏,这样才能更好的使用它们。

a、 比如:我在FragmentA中的EditText填了一些数据,当切换到FragmentB时,如果希望会到A还能看到数据,则适合你的就是hide和 show;也就是说,希望保留用户操作的面板,你可以使用hide和show,当然了不要使劲在那new实例,进行下非null判断。

b、再比如:我不希望保留用户操作,你可以使用remove(),然后add();或者使用replace()这个和remove,add是相同的效果。

c、 remove和detach有一点细微的区别,在不考虑回退栈的情况下,remove会销毁整个Fragment实例,而detach则只是销毁其视图结 构,实例并不会被销毁。那么二者怎么取舍使用呢?如果你的当前Activity一直存在,那么在不希望保留用户操作的时候,你可以优先使用detach。

上述已经介绍完成了Fragment常用的一些方法,相信看完,大家一定清楚了Fragment的产生理由,以及如何使用Fragment,再根据API的讲解,也能明白,曾经为何觉得Fragment会出现一些列乱七八槽的问题,终究是因为没有弄清楚其生命周期。

由于篇幅原因,剩下的内容留到下一篇了。在下一篇,会介绍:

1、如何管理Fragment回退栈

2、Fragment如何与Activity交互

3、Fragment与Activity交互的最佳实践

4、没有视图的Fragment的用处

5、使用Fragment创建对话框

6、如何与ActionBar,MenuItem集成等~~

Android Fragment 真正的完全解析(下) - Hongyang - 博客频道 - CSDN.NET

上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上)

本 篇将介绍上篇博客提到的:如何管理Fragment回退栈,Fragment如何与Activity交互,Fragment与Activity交互的最佳 实践,没有视图的Fragment的用处,使用Fragment创建对话框,如何与ActionBar,MenuItem集成等~~

1、管理Fragment回退栈

类 似与Android系统为Activity维护一个任务栈,我们也可以通过Activity维护一个回退栈来保存每次Fragment事务发生的变化。如 果你将Fragment任务添加到回退栈,当用户点击后退按钮时,将看到上一次的保存的Fragment。一旦Fragment完全从后退栈中弹出,用户 再次点击后退键,则退出当前Activity。

看这样一个效果图:

点 击第一个按钮,切换到第二个界面,点击第二个按钮,切换到第三个界面,然后点击Back键依次回退。这像不像初学Android时的Activity跳 转,当然了,这里肯定不是,不然我就跪了。这里是Fragment实现的,用户点击Back,实际是Fragment回退栈不断的弹栈。

如何添加一个Fragment事务到回退栈:

FragmentTransaction.addToBackStack(String)

下面讲解代码:很明显一共是3个Fragment和一个Activity.

先看Activity的布局文件:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <FrameLayout  
  7.         android:id="@+id/id_content"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent" >  
  10.     </FrameLayout>  
  11.   
  12. </RelativeLayout>  

不同的Fragment就在这个FrameLayout中显示。

MainActivity.java

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8.   
  9. public class MainActivity extends Activity  
  10. {  
  11.   
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         FragmentManager fm = getFragmentManager();  
  21.         FragmentTransaction tx = fm.beginTransaction();  
  22.         tx.add(R.id.id_content, new FragmentOne(),"ONE");  
  23.         tx.commit();  
  24.     }  
  25.   
  26. }  

很 简单,直接将FragmentOne添加到布局文件中的FrameLayout中,注意这里并没有调用 FragmentTransaction.addToBackStack(String),因为我不喜欢在当前显示时,点击Back键出现白板。而是正确 的相应Back键,即退出我们的Activity.

下面是FragmentOne

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup;  
  11. import android.widget.Button;  
  12.   
  13. public class FragmentOne extends Fragment implements OnClickListener  
  14. {  
  15.   
  16.     private Button mBtn;  
  17.   
  18.     @Override  
  19.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  20.             Bundle savedInstanceState)  
  21.     {  
  22.         View view = inflater.inflate(R.layout.fragment_one, container, false);  
  23.         mBtn = (Button) view.findViewById(R.id.id_fragment_one_btn);  
  24.         mBtn.setOnClickListener(this);  
  25.         return view;  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onClick(View v)  
  30.     {  
  31.         FragmentTwo fTwo = new FragmentTwo();  
  32.         FragmentManager fm = getFragmentManager();  
  33.         FragmentTransaction tx = fm.beginTransaction();  
  34.         tx.replace(R.id.id_content, fTwo, "TWO");  
  35.         tx.addToBackStack(null);  
  36.         tx.commit();  
  37.   
  38.     }  
  39.   
  40. }  


我 们在点击FragmentOne中的按钮时,使用了replace方法,如果你看了前一篇博客,一定记得replace是remove和add的合体,并 且如果不添加事务到回退栈,前一个Fragment实例会被销毁。这里很明显,我们调用tx.addToBackStack(null);将当前的事务添 加到了回退栈,所以FragmentOne实例不会被销毁,但是视图层次依然会被销毁,即会调用onDestoryView和onCreateView, 证据就是:仔细看上面的效果图,我们在跳转前在文本框输入的内容,在用户Back得到第一个界面的时候不见了。

接下来FragmentTwo

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup;  
  11. import android.widget.Button;  
  12.   
  13. public class FragmentTwo extends Fragment implements OnClickListener  
  14. {  
  15.   
  16.     private Button mBtn ;  
  17.     @Override  
  18.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  19.             Bundle savedInstanceState)  
  20.     {  
  21.         View view = inflater.inflate(R.layout.fragment_two, container, false);  
  22.         mBtn = (Button) view.findViewById(R.id.id_fragment_two_btn);  
  23.         mBtn.setOnClickListener(this);  
  24.         return view ;   
  25.     }  
  26.     @Override  
  27.     public void onClick(View v)  
  28.     {  
  29.         FragmentThree fThree = new FragmentThree();  
  30.         FragmentManager fm = getFragmentManager();  
  31.         FragmentTransaction tx = fm.beginTransaction();  
  32.         tx.hide(this);  
  33.         tx.add(R.id.id_content , fThree, "THREE");  
  34. //      tx.replace(R.id.id_content, fThree, "THREE");  
  35.         tx.addToBackStack(null);  
  36.         tx.commit();  
  37.     }  
  38.   
  39.   
  40. }  


这 里点击时,我们没有使用replace,而是先隐藏了当前的Fragment,然后添加了FragmentThree的实例,最后将事务添加到回退栈。这 样做的目的是为了给大家提供一种方案:如果不希望视图重绘该怎么做,请再次仔细看效果图,我们在FragmentTwo的EditText填写的内容,用 户Back回来时,数据还在~~~

最后FragmentThree就是简单的Toast了:

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11.   
  12. public class FragmentThree extends Fragment implements OnClickListener  
  13. {  
  14.   
  15.     private Button mBtn;  
  16.   
  17.     @Override  
  18.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  19.             Bundle savedInstanceState)  
  20.     {  
  21.         View view = inflater.inflate(R.layout.fragment_three, container, false);  
  22.         mBtn = (Button) view.findViewById(R.id.id_fragment_three_btn);  
  23.         mBtn.setOnClickListener(this);  
  24.         return view;  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onClick(View v)  
  29.     {  
  30.         Toast.makeText(getActivity(), " i am a btn in Fragment three",  
  31.                 Toast.LENGTH_SHORT).show();  
  32.     }  
  33.   
  34. }  


好了,经过上面的介绍,应该已经知道Fragment回退栈是怎么一回事了,以及hide,replace等各自的应用的场景。

这里极其注意一点:上面的整体代码不具有任何参考价值,纯粹为了显示回退栈,在后面讲解了Fragment与Activity通信以后,会重构上面的代码!

2、Fragment与Activity通信

因为所有的Fragment都是依附于Activity的,所以通信起来并不复杂,大概归纳为:

a、如果你Activity中包含自己管理的Fragment的引用,可以通过引用直接访问所有的Fragment的public方法

b、 如果Activity中未保存任何Fragment的引用,那么没关系,每个Fragment都有一个唯一的TAG或者ID,可以通过 getFragmentManager.findFragmentByTag()或者findFragmentById()获得任何Fragment实 例,然后进行操作。

c、在Fragment中可以通过getActivity得到当前绑定的Activity的实例,然后进行操作。

注:如果在Fragment中需要Context,可以通过调用getActivity(),如果该Context需要在Activity被销毁后还存在,则使用getActivity().getApplicationContext()。

3、Fragment与Activity通信的最佳实践

因为要考虑Fragment的重复使用,所以必须降低Fragment与Activity的耦合,而且Fragment更不应该直接操作别的Fragment,毕竟Fragment操作应该由它的管理者Activity来决定。

下面我通过两种方式的代码,分别重构,FragmentOne和FragmentTwo的点击事件,以及Activity对点击事件的响应:

首先看FragmentOne

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10.   
  11. public class FragmentOne extends Fragment implements OnClickListener  
  12. {  
  13.     private Button mBtn;  
  14.   
  15.     /** 
  16.      * 设置按钮点击的回调 
  17.      * @author zhy 
  18.      * 
  19.      */  
  20.     public interface FOneBtnClickListener  
  21.     {  
  22.         void onFOneBtnClick();  
  23.     }  
  24.   
  25.     @Override  
  26.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  27.             Bundle savedInstanceState)  
  28.     {  
  29.         View view = inflater.inflate(R.layout.fragment_one, container, false);  
  30.         mBtn = (Button) view.findViewById(R.id.id_fragment_one_btn);  
  31.         mBtn.setOnClickListener(this);  
  32.         return view;  
  33.     }  
  34.   
  35.     /** 
  36.      * 交给宿主Activity处理,如果它希望处理 
  37.      */  
  38.     @Override  
  39.     public void onClick(View v)  
  40.     {  
  41.         if (getActivity() instanceof FOneBtnClickListener)  
  42.         {  
  43.             ((FOneBtnClickListener) getActivity()).onFOneBtnClick();  
  44.         }  
  45.     }  
  46.   
  47. }  


可 以看到现在的FragmentOne不和任何Activity耦合,任何Activity都可以使用;并且我们声明了一个接口,来回调其点击事件,想要管 理其点击事件的Activity实现此接口就即可。可以看到我们在onClick中首先判断了当前绑定的Activity是否实现了该接口,如果实现了则 调用。

再看FragmentTwo

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10.   
  11. public class FragmentTwo extends Fragment implements OnClickListener  
  12. {  
  13.   
  14.       
  15.     private Button mBtn ;  
  16.       
  17.     private FTwoBtnClickListener fTwoBtnClickListener ;  
  18.       
  19.     public interface FTwoBtnClickListener  
  20.     {  
  21.         void onFTwoBtnClick();  
  22.     }  
  23.     //设置回调接口  
  24.     public void setfTwoBtnClickListener(FTwoBtnClickListener fTwoBtnClickListener)  
  25.     {  
  26.         this.fTwoBtnClickListener = fTwoBtnClickListener;  
  27.     }  
  28.     @Override  
  29.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  30.             Bundle savedInstanceState)  
  31.     {  
  32.         View view = inflater.inflate(R.layout.fragment_two, container, false);  
  33.         mBtn = (Button) view.findViewById(R.id.id_fragment_two_btn);  
  34.         mBtn.setOnClickListener(this);  
  35.         return view ;   
  36.     }  
  37.     @Override  
  38.     public void onClick(View v)  
  39.     {  
  40.         if(fTwoBtnClickListener != null)  
  41.         {  
  42.             fTwoBtnClickListener.onFTwoBtnClick();  
  43.         }  
  44.     }  
  45.   
  46. }  


与FragmentOne极其类似,但是我们提供了setListener这样的方法,意味着Activity不仅需要实现该接口,还必须显示调用mFTwo.setfTwoBtnClickListener(this)。

最后看Activity :

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8.   
  9. import com.zhy.zhy_fragments.FragmentOne.FOneBtnClickListener;  
  10. import com.zhy.zhy_fragments.FragmentTwo.FTwoBtnClickListener;  
  11.   
  12. public class MainActivity extends Activity implements FOneBtnClickListener,  
  13.         FTwoBtnClickListener  
  14. {  
  15.   
  16.     private FragmentOne mFOne;  
  17.     private FragmentTwo mFTwo;  
  18.     private FragmentThree mFThree;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState)  
  22.     {  
  23.         super.onCreate(savedInstanceState);  
  24.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  25.         setContentView(R.layout.activity_main);  
  26.   
  27.         mFOne = new FragmentOne();  
  28.         FragmentManager fm = getFragmentManager();  
  29.         FragmentTransaction tx = fm.beginTransaction();  
  30.         tx.add(R.id.id_content, mFOne, "ONE");  
  31.         tx.commit();  
  32.     }  
  33.   
  34.     /** 
  35.      * FragmentOne 按钮点击时的回调 
  36.      */  
  37.     @Override  
  38.     public void onFOneBtnClick()  
  39.     {  
  40.   
  41.         if (mFTwo == null)  
  42.         {  
  43.             mFTwo = new FragmentTwo();  
  44.             mFTwo.setfTwoBtnClickListener(this);  
  45.         }  
  46.         FragmentManager fm = getFragmentManager();  
  47.         FragmentTransaction tx = fm.beginTransaction();  
  48.         tx.replace(R.id.id_content, mFTwo, "TWO");  
  49.         tx.addToBackStack(null);  
  50.         tx.commit();  
  51.     }  
  52.   
  53.     /** 
  54.      * FragmentTwo 按钮点击时的回调 
  55.      */  
  56.     @Override  
  57.     public void onFTwoBtnClick()  
  58.     {  
  59.         if (mFThree == null)  
  60.         {  
  61.             mFThree = new FragmentThree();  
  62.   
  63.         }  
  64.         FragmentManager fm = getFragmentManager();  
  65.         FragmentTransaction tx = fm.beginTransaction();  
  66.         tx.hide(mFTwo);  
  67.         tx.add(R.id.id_content, mFThree, "THREE");  
  68.         // tx.replace(R.id.id_content, fThree, "THREE");  
  69.         tx.addToBackStack(null);  
  70.         tx.commit();  
  71.     }  
  72.   
  73. }  


代 码重构结束,与开始的效果一模一样。上面两种通信方式都是值得推荐的,随便选择一种自己喜欢的。这里再提一下:虽然Fragment和Activity可 以通过getActivity与findFragmentByTag或者findFragmentById,进行任何操作,甚至在Fragment里面操 作另外的Fragment,但是没有特殊理由是绝对不提倡的。Activity担任的是Fragment间类似总线一样的角色,应当由它决定 Fragment如何操作。另外虽然Fragment不能响应Intent打开,但是Activity可以,Activity可以接收Intent,然后 根据参数判断显示哪个Fragment。

4、如何处理运行时配置发生变化

运行时配置发生变化,最常见的就是屏幕发生旋转,如果你不知道如何处理屏幕变化可以参考:Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

这 里提一下:很多人觉得强制设置屏幕的方向就可以了,但是有一点,当你的应用被至于后台(例如用户点击了home),长时间没有返回的时候,你的应用也会被 重新启动。比如上例:如果你把上面的例子你至于FragmentThree界面,然后处于后台状态,长时间后你会发现当你再次通过home打开时,上面 FragmentThree与FragmentOne叠加在一起,这就是因为你的Activity重新启动,在原来的FragmentThree上又绘制 了一个FragmentOne。

好了,下面看一段代码:

Activity:

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8.   
  9. public class MainActivity extends Activity  
  10.   
  11. {  
  12.     private FragmentOne mFOne;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState)  
  16.     {  
  17.         super.onCreate(savedInstanceState);  
  18.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         mFOne = new FragmentOne();  
  22.         FragmentManager fm = getFragmentManager();  
  23.         FragmentTransaction tx = fm.beginTransaction();  
  24.         tx.add(R.id.id_content, mFOne, "ONE");  
  25.         tx.commit();  
  26.   
  27.     }  
  28.   
  29. }  


Fragment

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9.   
  10. public class FragmentOne extends Fragment  
  11. {  
  12.     private static final String TAG = "FragmentOne";  
  13.   
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  16.             Bundle savedInstanceState)  
  17.     {  
  18.         Log.e(TAG, "onCreateView");  
  19.         View view = inflater.inflate(R.layout.fragment_one, container, false);  
  20.         return view;  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState)  
  25.     {  
  26.         // TODO Auto-generated method stub  
  27.         super.onCreate(savedInstanceState);  
  28.   
  29.         Log.e(TAG, "onCreate");  
  30.     }  
  31.   
  32.     @Override  
  33.     public void onDestroyView()  
  34.     {  
  35.         // TODO Auto-generated method stub  
  36.         super.onDestroyView();  
  37.         Log.e(TAG, "onDestroyView");  
  38.     }  
  39.   
  40.     @Override  
  41.     public void onDestroy()  
  42.     {  
  43.         // TODO Auto-generated method stub  
  44.         super.onDestroy();  
  45.         Log.e(TAG, "onDestroy");  
  46.     }  
  47.   
  48. }  


很简单的代码,当你运行之后,不断的旋转屏幕,你会发现每旋转一次屏幕,屏幕上就多了一个FragmentOne的实例,并且后台log会打印出许多套生命周期的回调。

类似:

  1. 07-20 08:18:46.651: E/FragmentOne(1633): onCreate  
  2. 07-20 08:18:46.651: E/FragmentOne(1633): onCreate  
  3. 07-20 08:18:46.651: E/FragmentOne(1633): onCreate  
  4. 07-20 08:18:46.681: E/FragmentOne(1633): onCreateView  
  5. 07-20 08:18:46.831: E/FragmentOne(1633): onCreateView  
  6. 07-20 08:18:46.891: E/FragmentOne(1633): onCreateView  


这 是为什么呢,因为当屏幕发生旋转,Activity发生重新启动,默认的Activity中的Fragment也会跟着Activity重新创建;这样造 成当旋转的时候,本身存在的Fragment会重新启动,然后当执行Activity的onCreate时,又会再次实例化一个新的Fragment,这 就是出现的原因。

那么如何解决呢:

其实通过检查onCreate的参数Bundle savedInstanceState就可以判断,当前是否发生Activity的重新创建:

默认的savedInstanceState会存储一些数据,包括Fragment的实例:通过打印可以看出:

  1. 07-20 08:23:12.952: E/FragmentOne(1782): Bundle[{android:fragments=android.app.FragmentManagerState@40d0b7b8, android:viewHierarchyState=Bundle[{android:focusedViewId=2131230721, android:views=android.util.SparseArray@40d0af68}]}]  

所以,我们简单改一下代码,只有在savedInstanceState==null时,才进行创建Fragment实例:

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.Window;  
  9.   
  10. public class MainActivity extends Activity  
  11.   
  12. {  
  13.     private static final String TAG = "FragmentOne";  
  14.     private FragmentOne mFOne;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState)  
  18.     {  
  19.         super.onCreate(savedInstanceState);  
  20.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  21.         setContentView(R.layout.activity_main);  
  22.   
  23.         Log.e(TAG, savedInstanceState+"");  
  24.           
  25.         if(savedInstanceState == null)  
  26.         {  
  27.             mFOne = new FragmentOne();  
  28.             FragmentManager fm = getFragmentManager();  
  29.             FragmentTransaction tx = fm.beginTransaction();  
  30.             tx.add(R.id.id_content, mFOne, "ONE");  
  31.             tx.commit();  
  32.         }  
  33.           
  34.           
  35.   
  36.     }  
  37.   
  38. }  


现在无论进行多次旋转都只会有一个Fragment实例在Activity中。

现在还存在一个问题,就是重新绘制时,Fragment发生重建,原本的数据如何保持?

其实和Activity类似,Fragment也有onSaveInstanceState的方法,在此方法中进行保存数据,然后在onCreate或者onCreateView或者onActivityCreated进行恢复都可以。

由于篇幅原因,就不贴测试代码了。

5、Fragmeny与ActionBar和MenuItem集成

Fragment可以添加自己的MenuItem到Activity的ActionBar或者可选菜单中。

a、在Fragment的onCreate中调用setHasOptionsMenu(true);

b、然后在Fragment子类中实现onCreateOptionsMenu

c、如果希望在Fragment中处理MenuItem的点击,也可以实现onOptionsItemSelected;当然了Activity也可以直接处理该MenuItem的点击事件。

代码:

Fragment

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.Menu;  
  7. import android.view.MenuInflater;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.Toast;  
  12.   
  13. public class FragmentOne extends Fragment  
  14. {  
  15.   
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState)  
  18.     {  
  19.         super.onCreate(savedInstanceState);  
  20.         setHasOptionsMenu(true);  
  21.     }  
  22.   
  23.     @Override  
  24.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  25.             Bundle savedInstanceState)  
  26.     {  
  27.         View view = inflater.inflate(R.layout.fragment_one, container, false);  
  28.         return view;  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)  
  33.     {  
  34.         inflater.inflate(R.menu.fragment_menu, menu);  
  35.     }  
  36.   
  37.     @Override  
  38.     public boolean onOptionsItemSelected(MenuItem item)  
  39.     {  
  40.         switch (item.getItemId())  
  41.         {  
  42.         case R.id.id_menu_fra_test:  
  43.             Toast.makeText(getActivity(), "FragmentMenuItem1", Toast.LENGTH_SHORT).show();  
  44.             break;  
  45.         }  
  46.         return true;  
  47.     }  
  48.   
  49. }  


Activity

  1. package com.zhy.zhy_fragments;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10. import android.view.Window;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity  
  14.   
  15. {  
  16.     private static final String TAG = "FragmentOne";  
  17.     private FragmentOne mFOne;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState)  
  21.     {  
  22.         super.onCreate(savedInstanceState);  
  23.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  24.         setContentView(R.layout.activity_main);  
  25.   
  26.         Log.e(TAG, savedInstanceState + "");  
  27.   
  28.         if (savedInstanceState == null)  
  29.         {  
  30.             mFOne = new FragmentOne();  
  31.             FragmentManager fm = getFragmentManager();  
  32.             FragmentTransaction tx = fm.beginTransaction();  
  33.             tx.add(R.id.id_content, mFOne, "ONE");  
  34.             tx.commit();  
  35.         }  
  36.   
  37.     }  
  38.   
  39.     @Override  
  40.     public boolean onCreateOptionsMenu(Menu menu)  
  41.     {  
  42.         super.onCreateOptionsMenu(menu);  
  43.         getMenuInflater().inflate(R.menu.main, menu);  
  44.         return true;  
  45.     }  
  46.   
  47.     @Override  
  48.     public boolean onOptionsItemSelected(MenuItem item)  
  49.     {  
  50.         switch (item.getItemId())  
  51.         {  
  52.         case R.id.action_settings:  
  53.             Toast.makeText(this"setting", Toast.LENGTH_SHORT).show();  
  54.             return true;  
  55.         default:  
  56.             //如果希望Fragment自己处理MenuItem点击事件,一定不要忘了调用super.xxx  
  57.             return super.onOptionsItemSelected(item);  
  58.         }  
  59.     }  
  60.   
  61. }  


效果图:

好了,可以很好的看到,Fragment可以添加MenuItem,也可以自己处理点击~~~

6、没有布局的Fragment的作用

没有布局文件Fragment实际上是为了保存,当Activity重启时,保存大量数据准备的

请参考博客:Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

7、使用Fragment创建对话框

这是Google推荐的方式,我也单独写过博客介绍,请参考:Android 官方推荐 : DialogFragment 创建对话框

好了,终于把Fragment相关的联系到一起了,上述基本包含了Fragment所有的用法~~~相信大家如果能够看完,一定有不少的收获~~~

原文地址:https://www.cnblogs.com/seven1979/p/4203805.html