Fragment(四)Fragment生命周期分析(转)

Fragment(四)Fragment生命周期分析

转载请注明:http://blog.csdn.net/liaoqianchuan00/article/details/24271607

 

例子一 从Layout中加载Fragment:

 

假设我们有一个Activity的Layout中有两个Fragment:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.androidtest.MainActivity"

android:orientation="horizontal"

tools:ignore="MergeRootFrame" >

<fragmentandroid:name="com.example.androidtest.FragmentA"

android:id="@+id/fragmentA"

android:layout_weight="1"

android:layout_width="match_parent"

android:layout_height="match_parent" />

<fragment android:name="com.example.androidtest.FragmentB"

android:id="@+id/fragmentB"

android:layout_weight="1"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</LinearLayout>

而我们的Fragment很简单:

public class MainActivity extends FragmentActivity {

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.d("==========","MainActivity onCreate");

setContentView(R.layout.activity_main);

Log.d("==========","MainActivity onCreate finish");

}

@Override

protectedvoid onDestroy() {

Log.d("==========","MainActivity onDestroy");

super.onDestroy();

Log.d("==========","MainActivity onDestroy finish");

}

@Override

protectedvoid onPause() {

Log.d("==========","MainActivity onPause");

super.onPause();

Log.d("==========","MainActivity onPause finish");

}

@Override

protectedvoid onResume() {

Log.d("==========","MainActivity onResume");

super.onResume();

Log.d("==========","MainActivity onResume finish");

}

@Override

protectedvoid onStart() {

Log.d("==========","MainActivity onStart");

super.onStart();

Log.d("==========","MainActivity onStart finish");

}

@Override

protectedvoid onStop() {

Log.d("==========","MainActivity onStop");

super.onStop();

Log.d("==========","MainActivity onStop finish");

}

}

接下来是我们的FragmentA:

public class FragmentA extends Fragment {

@Override

publicvoid onAttach(Activity activity) {

super.onAttach(activity);

Log.d("==========","FragmentA onAttach");

}

@Override

publicvoid onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

Log.d("==========","FragmentA onActivityCreated");

}

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.d("==========","FragmentA onCreate");

}

@Override

publicView onCreateView(LayoutInflater inflater, ViewGroup container,

BundlesavedInstanceState) {

Log.d("==========","FragmentA onCreateView");

returninflater.inflate(R.layout.fragment_main, container, false);

}

@Override

publicvoid onDestroy() {

super.onDestroy();

Log.d("==========","FragmentA onDestroy");

}

@Override

publicvoid onDestroyView() {

super.onDestroyView();

Log.d("==========","FragmentA onDestroyView");

}

@Override

publicvoid onDetach() {

super.onDetach();

Log.d("==========","FragmentA onDetach");

}

@Override

publicvoid onPause() {

super.onPause();

Log.d("==========","FragmentA onPause");

}

@Override

publicvoid onResume() {

super.onResume();

Log.d("==========","FragmentA onResume");

}

@Override

publicvoid onStart() {

super.onStart();

Log.d("==========","FragmentA onStart");

}

@Override

publicvoid onStop() {

super.onStop();

Log.d("==========","FragmentA onStop");

}

}

Fragment B和A类似,只是log不同。

开始测试。运行程序,当出现界面之后,关闭屏幕电源,然后再点亮屏幕,出现屏幕之后再旋转屏幕,最后按back退出程序。

查看Log:

======运行程序

04-21 16:54:05.689: D/==========(31589):MainActivity onCreate

04-21 16:54:05.699: D/==========(31589):FragmentA onAttach

04-21 16:54:05.699: D/==========(31589):FragmentA onCreate

04-21 16:54:05.709: D/==========(31589):FragmentA onCreateView

04-21 16:54:05.709: D/==========(31589):FragmentB onAttach

04-21 16:54:05.709: D/==========(31589):FragmentB onCreate

04-21 16:54:05.709: D/==========(31589):FragmentB onCreateView

04-21 16:54:05.709: D/==========(31589):MainActivity onCreate finish

04-21 16:54:05.709: D/==========(31589):MainActivity onStart

04-21 16:54:05.709: D/==========(31589):FragmentA onActivityCreated

04-21 16:54:05.709: D/==========(31589):FragmentB onActivityCreated

04-21 16:54:05.709: D/==========(31589):FragmentA onStart

04-21 16:54:05.709: D/==========(31589):FragmentB onStart

04-21 16:54:05.709: D/==========(31589):MainActivity onStart finish

04-21 16:54:05.709: D/==========(31589):MainActivity onResume

04-21 16:54:05.709: D/==========(31589):MainActivity onResume finish

04-21 16:54:05.709: D/==========(31589):FragmentA onResume

04-21 16:54:05.709: D/==========(31589):FragmentB onResume

======关闭屏幕

04-21 16:54:09.929: D/==========(31589):MainActivity onPause

04-21 16:54:09.989: D/==========(31589):FragmentA onPause

04-21 16:54:09.989: D/==========(31589):FragmentB onPause

04-21 16:54:09.989: D/==========(31589):MainActivity onPause finish

04-21 16:54:09.999: D/==========(31589):MainActivity onStop

04-21 16:54:09.999: D/==========(31589):FragmentA onStop

04-21 16:54:09.999: D/==========(31589):FragmentB onStop

04-21 16:54:09.999: D/==========(31589):MainActivity onStop finish

======再次点亮屏幕

04-21 16:54:22.099: D/==========(31589):MainActivity onStart

04-21 16:54:22.099: D/==========(31589):FragmentA onStart

04-21 16:54:22.099: D/==========(31589):FragmentB onStart

04-21 16:54:22.099: D/==========(31589):MainActivity onStart finish

04-21 16:54:22.109: D/==========(31589):MainActivity onResume

04-21 16:54:22.109: D/==========(31589):MainActivity onResume finish

04-21 16:54:22.109: D/==========(31589):FragmentA onResume

04-21 16:54:22.109: D/==========(31589):FragmentB onResume

======旋转屏幕(导致重新创建Activity和Fragment)

04-21 16:54:26.729: D/==========(31589):MainActivity onPause

04-21 16:54:26.729: D/==========(31589):FragmentA onPause

04-21 16:54:26.729: D/==========(31589):FragmentB onPause

04-21 16:54:26.729: D/==========(31589):MainActivity onPause finish

04-21 16:54:26.729: D/==========(31589):MainActivity onStop

04-21 16:54:26.729: D/==========(31589):FragmentA onStop

04-21 16:54:26.729: D/==========(31589):FragmentB onStop

04-21 16:54:26.729: D/==========(31589):MainActivity onStop finish

04-21 16:54:26.729: D/==========(31589):MainActivity onDestroy

04-21 16:54:26.729: D/==========(31589):FragmentA onDestroyView

04-21 16:54:26.729: D/==========(31589):FragmentA onDestroy

04-21 16:54:26.729: D/==========(31589):FragmentA onDetach

04-21 16:54:26.729: D/==========(31589):FragmentB onDestroyView

04-21 16:54:26.729: D/==========(31589):FragmentB onDestroy

04-21 16:54:26.729: D/==========(31589):FragmentB onDetach

04-21 16:54:26.729: D/==========(31589):MainActivity onDestroy finish

04-21 16:54:26.739: D/==========(31589):MainActivity onCreate

04-21 16:54:26.749: D/==========(31589):FragmentA onAttach

04-21 16:54:26.749: D/==========(31589):FragmentA onCreate

04-21 16:54:26.749: D/==========(31589):FragmentA onCreateView

04-21 16:54:26.749: D/==========(31589):FragmentB onAttach

04-21 16:54:26.749: D/==========(31589):FragmentB onCreate

04-21 16:54:26.749: D/==========(31589):FragmentB onCreateView

04-21 16:54:26.749: D/==========(31589):MainActivity onCreate finish

04-21 16:54:26.749: D/==========(31589):MainActivity onStart

04-21 16:54:26.749: D/==========(31589):FragmentA onActivityCreated

04-21 16:54:26.749: D/==========(31589):FragmentB onActivityCreated

04-21 16:54:26.749: D/==========(31589):FragmentA onStart

04-21 16:54:26.749: D/==========(31589):FragmentB onStart

04-21 16:54:26.749: D/==========(31589):MainActivity onStart finish

04-21 16:54:26.749: D/==========(31589):MainActivity onResume

04-21 16:54:26.749: D/==========(31589):MainActivity onResume finish

04-21 16:54:26.749: D/==========(31589):FragmentA onResume

04-21 16:54:26.749: D/==========(31589):FragmentB onResume

04-21 16:54:26.759: E/ViewRootImpl(31589):sendUserActionEvent() mView == null

======按Back退出程序

04-21 16:54:33.249: D/==========(31589):MainActivity onPause

04-21 16:54:33.249: D/==========(31589):FragmentA onPause

04-21 16:54:33.249: D/==========(31589):FragmentB onPause

04-21 16:54:33.249: D/==========(31589):MainActivity onPause finish

04-21 16:54:33.439:W/IInputConnectionWrapper(31589): showStatusIcon on inactive InputConnection

04-21 16:54:33.469: D/==========(31589):MainActivity onStop

04-21 16:54:33.469: D/==========(31589):FragmentA onStop

04-21 16:54:33.469: D/==========(31589):FragmentB onStop

04-21 16:54:33.469: D/==========(31589):MainActivity onStop finish

04-21 16:54:33.469: D/==========(31589):MainActivity onDestroy

04-21 16:54:33.469: D/==========(31589):FragmentA onDestroyView

04-21 16:54:33.469: D/==========(31589):FragmentA onDestroy

04-21 16:54:33.469: D/==========(31589):FragmentA onDetach

04-21 16:54:33.469: D/==========(31589):FragmentB onDestroyView

04-21 16:54:33.469: D/==========(31589):FragmentB onDestroy

04-21 16:54:33.469: D/==========(31589):FragmentB onDetach

04-21 16:54:33.469: D/==========(31589):MainActivity onDestroy finish

例子二 从代码中加载Fragment

接下来我们将上面的例子改一下,在点击Activity界面一个Button的时候添加两个Fragment。

我们将Activity的layout文件改成如下所示:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.androidtest.MainActivity"

tools:ignore="MergeRootFrame" >

<Button

android:id="@+id/btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

</Button>

</LinearLayout>

将MainActivity的onCreate函数改成如下:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.d("==========","MainActivity onCreate");

setContentView(R.layout.activity_main);

Buttonbtn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(newOnClickListener() {

@Override

publicvoid onClick(View arg0) {

Fragmenta = new FragmentA();

FragmentTransactionft = getSupportFragmentManager().beginTransaction();

ft.add(R.id.container,a);

ft.commit();

}

});

Log.d("==========","MainActivity onCreate finish");

}

开始测试。运行程序,当出现界面之后,点击按钮,出现新的界面的之后,关闭屏幕电源,然后再点亮屏幕,出现屏幕之后再旋转屏幕,最后按back退出程序。

查看Log:

======运行程序

04-21 17:08:39.869: D/==========(2830):MainActivity onCreate

04-21 17:08:39.889: D/==========(2830):MainActivity onCreate finish

04-21 17:08:39.889: D/==========(2830):MainActivity onStart

04-21 17:08:39.889: D/==========(2830):MainActivity onStart finish

04-21 17:08:39.889: D/==========(2830):MainActivity onResume

04-21 17:08:39.889: D/==========(2830):MainActivity onResume finish

======点击按钮

04-21 17:08:46.039: D/==========(2830):FragmentA onAttach

04-21 17:08:46.039: D/==========(2830):FragmentA onCreate

04-21 17:08:46.039: D/==========(2830):FragmentA onCreateView

04-21 17:08:46.039: D/==========(2830): FragmentAonActivityCreated

04-21 17:08:46.039: D/==========(2830):FragmentA onStart

04-21 17:08:46.039: D/==========(2830):FragmentA onResume

======关闭屏幕

04-21 17:09:23.239: D/==========(2830):MainActivity onPause

04-21 17:09:23.329: D/==========(2830): FragmentAonPause

04-21 17:09:23.329: D/==========(2830):MainActivity onPause finish

04-21 17:09:23.429: D/==========(2830):MainActivity onStop

04-21 17:09:23.429: D/==========(2830):FragmentA onStop

04-21 17:09:23.429: D/==========(2830):MainActivity onStop finish

======再次点亮屏幕

04-21 17:09:46.779: D/==========(2830):MainActivity onStart

04-21 17:09:46.779: D/==========(2830):FragmentA onStart

04-21 17:09:46.779: D/==========(2830):MainActivity onStart finish

04-21 17:09:46.789: D/==========(2830): MainActivityonResume

04-21 17:09:46.789: D/==========(2830):MainActivity onResume finish

04-21 17:09:46.789: D/==========(2830):FragmentA onResume

======旋转屏幕

04-21 17:09:53.339: D/==========(2830):MainActivity onPause

04-21 17:09:53.339: D/==========(2830):FragmentA onPause

04-21 17:09:53.349: D/==========(2830):MainActivity onPause finish

04-21 17:09:53.349: D/==========(2830):MainActivity onStop

04-21 17:09:53.349: D/==========(2830):FragmentA onStop

04-21 17:09:53.349: D/==========(2830):MainActivity onStop finish

04-21 17:09:53.349: D/==========(2830):MainActivity onDestroy

04-21 17:09:53.349: D/==========(2830):FragmentA onDestroyView

04-21 17:09:53.349: D/==========(2830):FragmentA onDestroy

04-21 17:09:53.349: D/==========(2830):FragmentA onDetach

04-21 17:09:53.349: D/==========(2830):MainActivity onDestroy finish

04-21 17:09:53.359: D/==========(2830):FragmentA onAttach

04-21 17:09:53.359: D/==========(2830):FragmentA onCreate

04-21 17:09:53.359: D/==========(2830):MainActivity onCreate

04-21 17:09:53.379: D/==========(2830):MainActivity onCreate finish

04-21 17:09:53.379: D/==========(2830):MainActivity onStart

04-21 17:09:53.379: D/==========(2830):FragmentA onCreateView

04-21 17:09:53.379: D/==========(2830):FragmentA onActivityCreated

04-21 17:09:53.379: D/==========(2830):FragmentA onStart

04-21 17:09:53.379: D/==========(2830):MainActivity onStart finish

04-21 17:09:53.379: D/==========(2830):MainActivity onResume

04-21 17:09:53.379: D/==========(2830):MainActivity onResume finish

04-21 17:09:53.379: D/==========(2830):FragmentA onResume

04-21 17:09:53.399: E/ViewRootImpl(2830):sendUserActionEvent() mView == null

======按back退出程序

04-21 17:10:14.069: D/==========(2830):MainActivity onPause

04-21 17:10:14.069: D/==========(2830):FragmentA onPause

04-21 17:10:14.079: D/==========(2830):MainActivity onPause finish

04-21 17:10:14.249:W/IInputConnectionWrapper(2830): showStatusIcon on inactive InputConnection

04-21 17:10:14.269: D/==========(2830):MainActivity onStop

04-21 17:10:14.269: D/==========(2830):FragmentA onStop

04-21 17:10:14.269: D/==========(2830):MainActivity onStop finish

04-21 17:10:14.269: D/==========(2830):MainActivity onDestroy

04-21 17:10:14.269: D/==========(2830):FragmentA onDestroyView

04-21 17:10:14.269: D/==========(2830):FragmentA onDestroy

04-21 17:10:14.269: D/==========(2830):FragmentA onDetach

04-21 17:10:14.269: D/==========(2830):MainActivity onDestroy finish

例子三 Hide Fragment之后的生命周期

我们在main activity中添加两个按钮,一个add fragment,一个hidefragment

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.androidtest.MainActivity"

tools:ignore="MergeRootFrame" >

<Button

android:id="@+id/btn1"

android:text="add Fragment"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

</Button>

<Button

android:id="@+id/btn2"

android:text="hide Fragment"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

</Button>

</LinearLayout>

修改MainActivity的onCreate函数

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.d("==========","MainActivity onCreate");

setContentView(R.layout.activity_main);

Buttonbtn1 = (Button) findViewById(R.id.btn1);

btn1.setOnClickListener(newOnClickListener() {

@Override

publicvoid onClick(View arg0) {

Log.d("==========","Add Fragment");

Fragmenta = new FragmentA();

FragmentTransactionft = getSupportFragmentManager().beginTransaction();

ft.add(R.id.container,a,"Fragment A");

ft.commit();

}

});

Buttonbtn2 = (Button) findViewById(R.id.btn2);

btn2.setOnClickListener(newOnClickListener() {

@Override

publicvoid onClick(View arg0) {

Log.d("==========","Hide Fragment");

FragmentTransactionft = getSupportFragmentManager().beginTransaction();

ft.hide(getSupportFragmentManager().findFragmentByTag("FragmentA"));

ft.commit();

}

});

Log.d("==========","MainActivity onCreate finish");

}

开始测试。运行程序,当出现界面之后,点击add Fragment,出现新的界面的之后,再点击hide Fragment,然后关闭屏幕,接着点亮屏幕。

查看Log:

======运行程序

04-21 17:18:53.069: D/==========(3974):MainActivity onCreate

04-21 17:18:53.079: D/==========(3974):MainActivity onCreate finish

04-21 17:18:53.079: D/==========(3974):MainActivity onStart

04-21 17:18:53.079: D/==========(3974):MainActivity onStart finish

04-21 17:18:53.079: D/==========(3974):MainActivity onResume

04-21 17:18:53.079: D/==========(3974):MainActivity onResume finish

======点击add Fragment

04-21 17:18:55.729: D/==========(3974): AddFragment

04-21 17:18:55.729: D/==========(3974):FragmentA onAttach

04-21 17:18:55.729: D/==========(3974):FragmentA onCreate

04-21 17:18:55.729: D/==========(3974):FragmentA onCreateView

04-21 17:18:55.729: D/==========(3974):FragmentA onActivityCreated

04-21 17:18:55.729: D/==========(3974):FragmentA onStart

04-21 17:18:55.729: D/==========(3974):FragmentA onResume

======点击hide Fragment

04-21 17:19:03.409: D/==========(3974):Hide Fragment

======关闭屏幕

04-21 17:19:20.349: D/==========(3974):MainActivity onPause

04-21 17:19:20.369: D/==========(3974):FragmentA onPause

04-21 17:19:20.369: D/==========(3974):MainActivity onPause finish

04-21 17:19:20.379: D/==========(3974):MainActivity onStop

04-21 17:19:20.379: D/==========(3974):FragmentA onStop

04-21 17:19:20.379: D/==========(3974):MainActivity onStop finish

======点亮屏幕

04-21 17:19:35.079: D/==========(3974):MainActivity onStart

04-21 17:19:35.079: D/==========(3974):FragmentA onStart

04-21 17:19:35.079: D/==========(3974):MainActivity onStart finish

04-21 17:19:35.089: D/==========(3974):MainActivity onResume

04-21 17:19:35.089: D/==========(3974):MainActivity onResume finish

04-21 17:19:35.089: D/==========(3974):FragmentA onResume

我们发现虽然我们的Fragment被hide起来了,但是因为他没有被移除掉,所以他的生命周期还是会被宿主Activity影响。

上一篇:像table一样布局div的CSS属性详解


下一篇:安卓官方ViewPager与android.support.design.widget.TabLayout双向交互联动切换 。