详解fragment(一):从源代码看fragment的生命周期

前面讲了那么多xml,我们缓一下,现在讲从代码角度来看fragment的创建。

我们通常会在FragmentActivity中通过如下方法:

getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment, tag).commit();
getSupportFragmentManager().beginTransaction().add(R.id.content, fragment, tag).commit();

来调用新建或者取代一个fragment。那你知道这中间发生了什么事情吗,别急,让我们慢慢看代码。

FragmentActivity.java中有个方法:getSupportFragmentManager

public FragmentManager getSupportFragmentManager() {
        return mFragments;
    }

这个方法用于获取一个FragmentManager,那么这个mFragments是哪里来的呢,我们搜一下这个类,不难发现他的初始化是这个样子的:

    final FragmentManagerImpl mFragments = new FragmentManagerImpl();

从中我们就可以知道FragmentManager以及FragmentManagerImpl是父子类关系。

FragmentManagerImpl.java中:

final class FragmentManagerImpl extends FragmentManager

而且不用说也可以看出来,FragmentManagerImpl是FragmentManager的具体实现,然后自然而然的我们能找到代码:

public FragmentTransaction beginTransaction()
  {
    return new BackStackRecord(this);
  }

自然而然的我们要去找到BackStackRecord.java这个类了,代码如下:

public BackStackRecord(FragmentManagerImpl manager) {
        mManager = manager;
    }

接下来,我们看如何添加一个Fragment到Activity,无非是addFragment方法了,我们到FragmentTransaction中看一下(前面说过的Impl类是具体实现):

public void addFragment(Fragment paramFragment, boolean paramBoolean)
  {
    if (this.mAdded == null)
      this.mAdded = new ArrayList();
    if (DEBUG)
      Log.v("FragmentManager", "add: " + paramFragment);
    makeActive(paramFragment);
    if (!paramFragment.mDetached)
    {
      this.mAdded.add(paramFragment);
      paramFragment.mAdded = true;
      paramFragment.mRemoving = false;
      if ((paramFragment.mHasMenu) && (paramFragment.mMenuVisible))
        this.mNeedMenuInvalidate = true;
      if (paramBoolean)
        moveToState(paramFragment);
    }
  }

这里果然是有一个数组ArrayList在管理,ArrayList是啥?是动态数组,这就意味着他是可以动态添加的,不是栈,所以里面的Fragments可以随便获取以及插入。

其中最重要的那句话就是moveToState,其实现代码还是比较多的,我们贴一下看看:

void moveToState(Fragment paramFragment, int paramInt1, int paramInt2, int paramInt3)
  {
    if ((!paramFragment.mAdded) && (paramInt1 > 1))
      paramInt1 = 1;
    if ((paramFragment.mRemoving) && (paramInt1 > paramFragment.mState))
      paramInt1 = paramFragment.mState;
    if (paramFragment.mState < paramInt1)
    {
      if ((paramFragment.mFromLayout) && (!paramFragment.mInLayout))
        return;
      if (paramFragment.mAnimatingAway != null)
      {
        paramFragment.mAnimatingAway = null;
        moveToState(paramFragment, paramFragment.mStateAfterAnimating, 0, 0);
      }
      switch (paramFragment.mState)
      {
      default:
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      }
    }
    while (true)
    {
      paramFragment.mState = paramInt1;
      break;
      if (DEBUG)
        Log.v("FragmentManager", "moveto CREATED: " + paramFragment);
      if (paramFragment.mSavedFragmentState != null)
      {
        paramFragment.mSavedViewState = paramFragment.mSavedFragmentState.getSparseParcelableArray("android:view_state");
        paramFragment.mTarget = getFragment(paramFragment.mSavedFragmentState, "android:target_state");
        if (paramFragment.mTarget != null)
          paramFragment.mTargetRequestCode = paramFragment.mSavedFragmentState.getInt("android:target_req_state", 0);
      }
      paramFragment.mActivity = this.mActivity;
      paramFragment.mFragmentManager = this.mActivity.mFragments;
      paramFragment.mCalled = false;
      paramFragment.onAttach(this.mActivity);
      if (!paramFragment.mCalled)
        throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onAttach()");
        //第一步:attach
      this.mActivity.onAttachFragment(paramFragment);
      if (!paramFragment.mRetaining)
      {
        paramFragment.mCalled = false;
        //第二步:onCreate
        paramFragment.onCreate(paramFragment.mSavedFragmentState);
        if (!paramFragment.mCalled)
          throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onCreate()");
      }
      paramFragment.mRetaining = false;
      if (paramFragment.mFromLayout)
      {
      //第三步:onCreateView
        paramFragment.mView = paramFragment.onCreateView(paramFragment.getLayoutInflater(paramFragment.mSavedFragmentState), null, paramFragment.mSavedFragmentState);
        if (paramFragment.mView == null)
          break label570;
        paramFragment.mInnerView = paramFragment.mView;
        paramFragment.mView = NoSaveStateFrameLayout.wrap(paramFragment.mView);
        if (paramFragment.mHidden)
          paramFragment.mView.setVisibility(8);
        paramFragment.onViewCreated(paramFragment.mView, paramFragment.mSavedFragmentState);
      }
      while (true)
      {
        if (paramInt1 <= 1)
          break label777;
        if (DEBUG)
          Log.v("FragmentManager", "moveto ACTIVITY_CREATED: " + paramFragment);
        if (paramFragment.mFromLayout)
          break;
        ViewGroup localViewGroup = null;
        if (paramFragment.mContainerId != 0)
        {
          localViewGroup = (ViewGroup)this.mActivity.findViewById(paramFragment.mContainerId);
          if ((localViewGroup == null) && (!paramFragment.mRestored))
          {
            throw new IllegalArgumentException("No view found for id 0x" + Integer.toHexString(paramFragment.mContainerId) + " for fragment " + paramFragment);
            label570: paramFragment.mInnerView = null;
            continue;
          }
        }
        paramFragment.mContainer = localViewGroup;
        paramFragment.mView = paramFragment.onCreateView(paramFragment.getLayoutInflater(paramFragment.mSavedFragmentState), localViewGroup, paramFragment.mSavedFragmentState);
        if (paramFragment.mView == null)
          break label753;
        paramFragment.mInnerView = paramFragment.mView;
        paramFragment.mView = NoSaveStateFrameLayout.wrap(paramFragment.mView);
        if (localViewGroup != null)
        {
          Animation localAnimation2 = loadAnimation(paramFragment, paramInt2, true, paramInt3);
          if (localAnimation2 != null)
            paramFragment.mView.startAnimation(localAnimation2);
          localViewGroup.addView(paramFragment.mView);
        }
        if (paramFragment.mHidden)
          paramFragment.mView.setVisibility(8);
        paramFragment.onViewCreated(paramFragment.mView, paramFragment.mSavedFragmentState);
      }
      while (true)
      {
        paramFragment.mCalled = false;
        //第四步:onActivityCreated
        paramFragment.onActivityCreated(paramFragment.mSavedFragmentState);
        if (paramFragment.mCalled)
          break;
        throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onActivityCreated()");
        label753: paramFragment.mInnerView = null;
      }
      if (paramFragment.mView != null)
        paramFragment.restoreViewState();
      paramFragment.mSavedFragmentState = null;
      label777: if (paramInt1 > 3)
      {
        if (DEBUG)
          Log.v("FragmentManager", "moveto STARTED: " + paramFragment);
        paramFragment.mCalled = false;
        paramFragment.performStart();
        if (!paramFragment.mCalled)
          throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onStart()");
      }
      if (paramInt1 <= 4)
        continue;
      if (DEBUG)
        Log.v("FragmentManager", "moveto RESUMED: " + paramFragment);
      paramFragment.mCalled = false;
      paramFragment.mResumed = true;
      //第五步:onResume
      paramFragment.onResume();
      if (!paramFragment.mCalled)
        throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onResume()");
      paramFragment.mSavedFragmentState = null;
      paramFragment.mSavedViewState = null;
      continue;
      if (paramFragment.mState <= paramInt1)
        continue;
      switch (paramFragment.mState)
      {
      default:
        break;
      case 1:
      case 5:
      case 4:
      case 3:
      case 2:
        while (paramInt1 < 1)
        {
          if ((this.mDestroyed) && (paramFragment.mAnimatingAway != null))
          {
            View localView = paramFragment.mAnimatingAway;
            paramFragment.mAnimatingAway = null;
            localView.clearAnimation();
          }
          if (paramFragment.mAnimatingAway == null)
            break label1519;
          paramFragment.mStateAfterAnimating = paramInt1;
          paramInt1 = 1;
          break;
          if (paramInt1 < 5)
          {
            if (DEBUG)
              Log.v("FragmentManager", "movefrom RESUMED: " + paramFragment);
            paramFragment.mCalled = false;
            //第六步:onPause
            paramFragment.onPause();
            if (!paramFragment.mCalled)
              throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onPause()");
            paramFragment.mResumed = false;
          }
          if (paramInt1 < 4)
          {
            if (DEBUG)
              Log.v("FragmentManager", "movefrom STARTED: " + paramFragment);
            paramFragment.mCalled = false;
            paramFragment.performStop();
            if (!paramFragment.mCalled)
              throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onStop()");
          }
          if (paramInt1 < 3)
          {
            if (DEBUG)
              Log.v("FragmentManager", "movefrom STOPPED: " + paramFragment);
            paramFragment.performReallyStop();
          }
          if (paramInt1 >= 2)
            continue;
          if (DEBUG)
            Log.v("FragmentManager", "movefrom ACTIVITY_CREATED: " + paramFragment);
          if ((paramFragment.mView != null) && (!this.mActivity.isFinishing()) && (paramFragment.mSavedViewState == null))
            saveFragmentViewState(paramFragment);
          paramFragment.mCalled = false;
          paramFragment.performDestroyView();
          if (!paramFragment.mCalled)
            throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onDestroyView()");
          if ((paramFragment.mView != null) && (paramFragment.mContainer != null))
          {
            Animation localAnimation1 = null;
            if ((this.mCurState > 0) && (!this.mDestroyed))
              localAnimation1 = loadAnimation(paramFragment, paramInt2, false, paramInt3);
            if (localAnimation1 != null)
            {
              paramFragment.mAnimatingAway = paramFragment.mView;
              paramFragment.mStateAfterAnimating = paramInt1;
              localAnimation1.setAnimationListener(new Animation.AnimationListener(paramFragment)
              {
                public void onAnimationEnd(Animation paramAnimation)
                {
                  if (this.val$fragment.mAnimatingAway != null)
                  {
                    this.val$fragment.mAnimatingAway = null;
                    FragmentManagerImpl.this.moveToState(this.val$fragment, this.val$fragment.mStateAfterAnimating, 0, 0);
                  }
                }

                public void onAnimationRepeat(Animation paramAnimation)
                {
                }

                public void onAnimationStart(Animation paramAnimation)
                {
                }
              });
              paramFragment.mView.startAnimation(localAnimation1);
            }
            paramFragment.mContainer.removeView(paramFragment.mView);
          }
          paramFragment.mContainer = null;
          paramFragment.mView = null;
          paramFragment.mInnerView = null;
        }
        label1519: if (DEBUG)
          Log.v("FragmentManager", "movefrom CREATED: " + paramFragment);
        if (!paramFragment.mRetaining)
        {
          paramFragment.mCalled = false;
          //第八步:onDestroy
          paramFragment.onDestroy();
          if (!paramFragment.mCalled)
            throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onDestroy()");
        }
        paramFragment.mCalled = false;
        //第九步:onDetach
        paramFragment.onDetach();
        if (!paramFragment.mCalled)
          throw new SuperNotCalledException("Fragment " + paramFragment + " did not call through to super.onDetach()");
        if (!paramFragment.mRetaining)
        {
          makeInactive(paramFragment);
          continue;
        }
        paramFragment.mActivity = null;
        paramFragment.mFragmentManager = null;
      }
    }
  }

本来不想贴这部分的代码的,但确实这个比较重要,还是看一下吧。

最后一步肯定是commit了,这个方法是由FragmentTransaction.java类调用的,其实包括前面的addFragment等都是在这个类调用的,可是他是个抽象类,具体的实现还是在Impl类中。我们还是稍微浏览一下这个类吧(只截取了部分):

public abstract class FragmentTransaction {

    public abstract FragmentTransaction add(int containerViewId, Fragment fragment);

    public abstract FragmentTransaction add(int containerViewId, Fragment fragment, String tag);

    public abstract FragmentTransaction replace(int containerViewId, Fragment fragment);

    public abstract FragmentTransaction replace(int containerViewId, Fragment fragment, String tag);

    public abstract FragmentTransaction remove(Fragment fragment);

    public abstract FragmentTransaction hide(Fragment fragment);

    public abstract FragmentTransaction show(Fragment fragment);

    public abstract FragmentTransaction detach(Fragment fragment);

    public abstract FragmentTransaction attach(Fragment fragment);

    public abstract int commit();
}

最后看commit的实现,本人目前还没找到,求告诉!

详解fragment(一):从源代码看fragment的生命周期

时间: 2024-10-16 04:12:12

详解fragment(一):从源代码看fragment的生命周期的相关文章

ARP协议详解之ARP动态与静态条目的生命周期

ARP协议详解之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自动添加和删除. q  每个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉.所以,生命周期也被称为超时值. q  延长规则:当ARP条目已存在,使用该条目后,将会重设超时值为两分钟. [实例1-12]下面将验证动态条目的生命周期是两分钟.具体操作步骤如下所示: (1)查看本机的ARP缓存表.执行命令如下所示: C:\Documents and Settings\Administra

Android——Fragment和Activity之间的通信+Frangment生命周期

Android--Fragment和Activity之间的通信+Frangment生命周期 Fr'agment和Activity之间的通信 1.在Fragment中声明一个接口. 2.在Activity中实现在Fargment中声明的接口. 3.在Fragment中声明一个接口对象. 4.在Frangment的生命周期Onattach方法中判断当前Activity是否实现了此Fragment中声明的接口.如果已实现,就把当前Activity转换成接口对象. 5.调用Activity中实现的方法=

C++多态篇2——虚函数表详解之从内存布局看函数重载,函数覆盖,函数隐藏

上一篇C++多态篇1一静态联编,动态联编.虚函数与虚函数表vtable中,我在最后分析了虚函数与虚函数表的内存布局,在下一篇详细剖析虚函数及虚函数表的过程中,我发现有关函数重载,函数覆盖,函数重写和函数协变的知识也要理解清楚才能对虚函数表在内存中的布局,对派生类的对象模型以及对多态的实现有更深的理解. 所以这一篇我作为一篇过渡篇,也同时对我以前写过的一篇博文进行一个收尾.在C++继承详解之二--派生类成员函数详解(函数隐藏.构造函数与兼容覆盖规则)文章中,我对函数覆盖,重载,重写提了一下,但是没

Android Fragment 隐藏或显示时调用的生命周期方法

Fragment使用方式大体分两种: 大家要注意不同的Fragment使用方法,Fragment隐藏和显示调用的生命周期方法是不同的,以下是Fragment显示隐藏调用的方法: //判断是否展示—与ViewPager连用,进行左右切换@Overridepublic void setUserVisibleHint(boolean isVisibleToUser) {super.setUserVisibleHint(isVisibleToUser);if (isVisibleToUser)//展示}

使用freemarker生成word,步骤详解并奉上源代码

1.   步骤 1.    用word编辑好模板 1. 普通字符串替换为 ${string} 2. 表格循环用标签 <#list userList as user> 姓名:${user.userName} , 性别:${user.sex} </#list> word模板原型如下图: 最终生成的结果如下: 2.    将word模板另存为xml格式 选中 [ 仅保存数据 ] 选择 [ 保持wordML(K) ] 3.    将xml模板文件后缀名改为.ftl 4.    编辑ftl文

最完整的Mybatis Generator(简称MBG)的最完整配置文件,带详解,再也不用去看EN的User Guide了;

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <

Android艺术——深看Activity的生命周期

探究Activity的生命周期 1.典型情况下的生命周期分析:onCreate 初始化工作,加载布局资源和数据:onStart ac正在启动但是无法交互,后台:onResume ac可见,显示在前台:onPause 正在停止,必须先执行完才能执行新的ac:Onstop 即将停止,回收工作:onDestory 即将销毁,回收与资源释放:onRestart 重新启动 2.onStart和onResume.onPause和onStop,有什么实质的不同. 答:onStart和onResume是从Act

Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)

            如需转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai       联系方式:[email protected]       [Android四大组件学习系列Activity篇]        1.Android四大组件之——Activity(一)定义.状态和后退栈(图文详解) 2.Android四大组件之——Activity的生命周期(图文详解) 3.Android四大组件之——Activity的开启StartActivit

Android Fragmnet详解

Fragment简介 Fragment 表示 Activity 中的行为或用户界面部分.您可以将多个Fragment组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个Fragment.您可以将Fragment视为Activity 的模块化组成部分,它具有自己的生命周期,能接收自己的输入事件,并且您可以在 Activity 运行时添加或删除Fragment(有点像您可以在不同 Activity 中重复使用的"子Activity"). Frag