标签:
Android之Activity,Fragment生命周期探知 - sin@ - 博客频道 - CSDN.NET
根据官方文档,本人手画了一下Activity和Fragment的整个生命周期执行顺序图:
Activity生命周期执行顺序图:
Fragment生命周期执行顺序图:
?一个Activity在它的整个生命周期里面分三个状态,分别是活动周期,可见周期,以及生命周期。 一个Fragment是依附于Activity之上,它的整个生命周期里面也分三个状态,分别是活动周期,可见周期,以及生命周期。 现在我们来分析一下这些状态的实际执行顺序,我们通过日志的方式来打印每个函数的执行过程,新建一个android工程,Activity代码和Fragment代码如下:
- public?class?MainActivity?extends?Activity?{??
- ??
- ????private?static?String?TAG?=?"chenjianjun";??
- ??????
- ????private?static?Button?mybtnButton;??
- ??????
- ????@Override??
- ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
- ????????//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.??
- ????????getMenuInflater().inflate(R.menu.main,?menu);??
- ????????return?true;??
- ????}??
- ??????
- ????/**??
- ?????*?在完整生存期开始时调用?
- ?????*?@see?android.app.Activity#onCreate(android.os.Bundle)?
- ?????*/??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_main);??
- ??????????
- ????????mybtnButton?=?(Button)findViewById(R.id.btn);??
- ????????mybtnButton.setOnClickListener(new?View.OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?v)?{??
- ????????????????Intent?bintent?=?new?Intent(MainActivity.this,?SecondActivity.class);??
- ????????????????startActivity(bintent);??
- ????????????}??
- ????????});??
- ??????????
- ????????Log.i(TAG,?"onCreate?is?call.......");??
- ????}??
- ??
- ????/**?
- ?????*?在onCreate方法后调用,用于恢复UI状态?
- ?????*?@see?android.app.Activity#onRestoreInstanceState(android.os.Bundle)?
- ?????*/??
- ????@Override??
- ????protected?void?onRestoreInstanceState(Bundle?savedInstanceState)??
- ????{??
- ????????Log.i(TAG,?"onRestoreInstanceState?is?call?start.......");??
- ????????super.onRestoreInstanceState(savedInstanceState);??
- ????????/**?
- ?????????*?从savedInstanceState恢复UI状态?
- ?????????*?这个savedInstanceState也被传递给了onCreate?
- ?????????*?自Activity上次可见之后,只有当系统终止了该Activity时,才会被调用?
- ?????????*?在随后的Activity进程的可见生存期之前被调用?
- ?????????*/??
- ????????Log.i(TAG,?"onRestoreInstanceState?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在可见生存期开始时调用?
- ?????*?@see?android.app.Activity#onStart()?
- ?????*/??
- ????@Override??
- ????protected?void?onStart()??
- ????{??
- ????????Log.i(TAG,?"onStart?is?call?start.......");??
- ????????super.onStart();??
- ????????/**?
- ?????????*?既然Activity可见,就应用任何要求的UI变化?
- ?????????*/??
- ????????Log.i(TAG,?"onStart?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在Activity活动状态生存期开始时调用?
- ?????*?@see?android.app.Activity#onResume()?
- ?????*/??
- ????@Override??
- ????protected?void?onResume()??
- ????{??
- ????????Log.i(TAG,?"onResume?is?call?start.......");??
- ????????super.onResume();??
- ????????/**?
- ?????????*?恢复Activity处于不活动状态时被挂起的,暂停的UI更新,线程,进程?
- ?????????*/??
- ????????Log.i(TAG,?"onResume?is?call?end.......");??
- ????}??
- ??????
- ????/*?
- ?????*?把UI状态保存到outState中?
- ?????*?@see?android.app.Activity#onSaveInstanceState(android.os.Bundle)?
- ?????*/??
- ????@Override??
- ????protected?void?onSaveInstanceState(Bundle?savedInstanceState)??
- ????{??
- ????????Log.i(TAG,?"onSaveInstanceState?is?call?start.......");??
- ????????/**?
- ?????????*?保存UI的状态?
- ?????????*?如果进程被运行时终止并重启,那么这个Bundle会被传递给onCreate和onRestoreInstanceState?
- ?????????*/??
- ????????super.onSaveInstanceState(savedInstanceState);??
- ????????Log.i(TAG,?"onSaveInstanceState?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在Activity活动状态生存期结束时被调用?
- ?????*/??
- ????@Override??
- ????protected?void?onPause()??
- ????{??
- ????????Log.i(TAG,?"onPause?is?call?start.......");??
- ????????/**?
- ?????????*?挂起UI更新,线程或者CPU密集的进程?
- ?????????*/??
- ????????super.onPause();??
- ????????Log.i(TAG,?"onPause?is?call?end.......");??
- ????}??
- ??????
- ????@Override??
- ????protected?void?onStop()??
- ????{??
- ????????Log.i(TAG,?"onStop?is?call?start.......");??
- ????????/**?
- ?????????*?挂起UI更新,线程或者处理?
- ?????????*?当Activity不可见时,保存所有的编辑或者状态改变?
- ?????????*?调用这个方法后,进程可能会被终止?
- ?????????*/??
- ????????super.onStop();??
- ??????????
- ????????Log.i(TAG,?"onStop?is?callv?end.......");??
- ????}??
- ??????
- ????@Override??
- ????protected?void?onDestroy()??
- ????{??
- ????????Log.i(TAG,?"onDestroy?is?call?start.......");??
- ????????/**?
- ?????????*?清理所有的资源,包括结束线程,关闭数据库连接等?
- ?????????*/??
- ????????super.onDestroy();??
- ??????????
- ????????Log.i(TAG,?"onDestroy?is?call?end.......");??
- ????}??
- ??????
- ????@Override??
- ????protected?void?onRestart()??
- ????{??
- ????????Log.i(TAG,?"onRestart?is?call?start.......");??
- ????????super.onRestart();??
- ????????/**?
- ?????????*?加装载改变,知道Activity在此进程中已经可见?
- ?????????*/??
- ????????Log.i(TAG,?"onRestart?is?call?end.......");??
- ????}??
- }??
?
- public?class?MyStateFragment?extends?Fragment?{??
- ??
- ????private?static?String?TAG?=?"chenjianjun";??
- ????/**?
- ?????*?调用该方法时,Fragment会被连接到它的父Activity上?
- ?????*/??
- ????@Override??
- ????public?void?onAttach(Activity?activity)??
- ????{??
- ????????Log.i(TAG,?"-----onAttach?is?call?start.......");??
- ????????super.onAttach(activity);??
- ????????/**?
- ?????????*?获取对父Activity的引用?
- ?????????*/??
- ????????Log.i(TAG,?"-----onAttach?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?调用该方法来进行Fragment的初始创建?
- ?????*/??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)??
- ????{??
- ????????Log.i(TAG,?"-----onCreate?is?call?start.......");??
- ????????super.onCreate(savedInstanceState);??
- ????????/**?
- ?????????*?初期化Fragment?
- ?????????*/??
- ????????Log.i(TAG,?"-----onCreate?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?一旦Fragment已被创建,要创建它自己的用户界面时调用该方法?
- ?????*/??
- ????@Override??
- ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?Bundle?savedInstanceState)??
- ????{??
- ????????Log.i(TAG,?"-----onCreateView?is?call?start.......");??
- ????????/**?
- ?????????*?创建,或者填充Fragment的UI,并返回它?
- ?????????*?如果这个Fragment没有UI,那么返回null?
- ?????????*/??
- ????????View?view?=?inflater.inflate(R.layout.my_state_fragment,?container,?false);??
- ??????????
- ????????Log.i(TAG,?"-----onCreateView?is?call?end.......");??
- ????????return?view;??
- ????}??
- ??????
- ????/**?
- ?????*?一旦父Activity和Fragment的UI已经被创建,则调用该方法?
- ?????*/??
- ????@Override??
- ????public?void?onActivityCreated(Bundle?savedInstanceState)??
- ????{??
- ????????Log.i(TAG,?"-----onActivityCreated?is?call?start.......");??
- ????????super.onActivityCreated(savedInstanceState);??
- ????????/**?
- ?????????*?完成Fragment的初期化----那些父Activity和Fragment的View被完全填充后才能做的事情?
- ?????????*/??
- ????????Log.i(TAG,?"-----onActivityCreated?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在可见生命周期的开始时被调用?
- ?????*/??
- ????@Override??
- ????public?void?onStart()??
- ????{??
- ????????Log.i(TAG,?"-----onStart?is?call?start.......");??
- ????????super.onStart();??
- ????????/**?
- ?????????*?应用所有需要的UI变化,现在Fragment是可见的?
- ?????????*/??
- ????????Log.i(TAG,?"-----onStart?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在活动生命周期开始时被调用?
- ?????*/??
- ????@Override??
- ????public?void?onResume()??
- ????{??
- ????????Log.i(TAG,?"-----onResume?is?call?start.......");??
- ????????super.onResume();??
- ????????/**?
- ?????????*?恢复所有暂停的事物,如UI更新,线程或者进程等?
- ?????????*/??
- ????????Log.i(TAG,?"-----onResume?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在活动生命周期结束时被调用?
- ?????*/??
- ????@Override??
- ????public?void?onPause()??
- ????{??
- ????????Log.i(TAG,?"-----onPause?is?call?start.......");??
- ????????/**?
- ?????????*?需要暂停父Activity不是活动的前台Activity时,需要暂停的事物,如UI更新,线程或者进程等?
- ?????????*/??
- ????????super.onPause();??
- ????????Log.i(TAG,?"-----onPause?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*??在活动生命周期结束时被调用,保存UI的状态变化?
- ?????*/??
- ????@Override??
- ????public?void?onSaveInstanceState(Bundle?savedInstanceState)??
- ????{??
- ????????Log.i(TAG,?"-----onSaveInstanceState?is?call?start.......");??
- ????????/**?
- ?????????*?将UI的变化信息保存到savedInstanceState中?
- ?????????*?这个savedInstanceState会被传递到onCreate,onCreateView和onActivityCreated(如果它的父Activity被终止并且重新启动)方法中?
- ?????????*/??
- ????????super.onSaveInstanceState(savedInstanceState);??
- ????????Log.i(TAG,?"-----onSaveInstanceState?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在可见生命周期结束时调用该方法?
- ?????*/??
- ????@Override??
- ????public?void?onStop()??
- ????{??
- ????????Log.i(TAG,?"-----onStop?is?call?start.......");??
- ????????/**?
- ?????????*?当Fragment不可见时,暂停其余的事物,如UI更新,线程或者进程等?
- ?????????*/??
- ????????super.onStop();??
- ????????Log.i(TAG,?"-----onStop?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?当Fragment的view被分离时,调用该方法?
- ?????*/??
- ????@Override??
- ????public?void?onDestroyView()??
- ????{??
- ????????Log.i(TAG,?"-----onDestroyView?is?call?start.......");??
- ????????/**?
- ?????????*?清除资源相关的view?
- ?????????*/??
- ????????super.onDestroyView();??
- ????????Log.i(TAG,?"-----onDestroyView?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?在整个生命周期结束的时候调用?
- ?????*/??
- ????@Override??
- ????public?void?onDestroy()??
- ????{??
- ????????Log.i(TAG,?"-----onDestroy?is?call?start.......");??
- ????????/**?
- ?????????*?清除所有的资源,包括线程和关闭数据库连接等?
- ?????????*/??
- ????????super.onDestroy();??
- ????????Log.i(TAG,?"-----onDestroy?is?call?end.......");??
- ????}??
- ??????
- ????/**?
- ?????*?当Fragment从它的父Activity上分离时调用?
- ?????*/??
- ????@Override??
- ????public?void?onDetach()??
- ????{??
- ????????Log.i(TAG,?"-----onDetach?is?call?start.......");??
- ????????super.onDetach();??
- ????????Log.i(TAG,?"-----onDetach?is?call?end.......");??
- ????}??
- }??
activity_main.xml:
- <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????xmlns:tools="http://schemas.android.com/tools"??
- ????android:layout_width="match_parent"??
- ????android:layout_height="match_parent"??
- ????android:paddingBottom="@dimen/activity_vertical_margin"??
- ????android:paddingLeft="@dimen/activity_horizontal_margin"??
- ????android:paddingRight="@dimen/activity_horizontal_margin"??
- ????android:paddingTop="@dimen/activity_vertical_margin"??
- ????tools:context=".MainActivity"?>??
- ??
- ????<fragment???
- ????????android:name="com.xuexi.mystateactivity.fragment.MyStateFragment"??
- ????????android:id="@+id/ToDoListFragment"??
- ????????android:layout_width="match_parent"??
- ????????android:layout_height="wrap_content"??
- ????????android:visibility="gone"???
- ????????/>??
- ??????
- ????<Button??
- ????????android:id="@+id/btn"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_alignLeft="@+id/ToDoListFragment"??
- ????????android:layout_below="@+id/ToDoListFragment"??
- ????????android:layout_marginTop="44dp"??
- ????????android:text="@string/btn_name"?/>??
- ??
- </RelativeLayout>??
编译运行,启动程序打印日志如下:切换到后台,日志如下:
从后台切换到前台,日志如下:
切换到其他的activity,日志如下:
从其他的activity切换回来,日志如下:
按home键退出,日志如下:
综上,大家对照代码查看日志的,我们展现了在每种情况下,Activity和Fragment的周期处于那种状态。理解好Activity和Fragment的生命周期,对于我们开发好一个app程序
有很大的帮助。
这里有个问题,跟文档上面有一点不相符的,就是Activity的onSaveInstanceState调用,在切换到后台和切换到其他Activity的时候有调用,但是都是在onPause和onStop之间,
书中的文字表述也是这样来说的,不知道为什么图是这样来画的!
Android之Activity,Fragment生命周期探知
标签:
原文地址:http://www.cnblogs.com/seven1979/p/4345121.html