Fragment的应用很广泛,但是Fragment必须得依赖Activity才能生存,Activity和Fragment的声明周期也惊人的相似。
下面通过一个demo来观擦Activity和Fragment的声明周期:
MainActivity.java代码如下:
public class MainActivity extends Activity { private final String TAG = "MainActivity"; private FragmentManager manager; private FragmentTransaction transaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = getFragmentManager(); transaction = manager.beginTransaction(); MyFragment fragment = new MyFragment(); transaction.add(R.id.line, fragment); transaction.commit(); Log.i(TAG, "--MainActivity->>onCreate"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i(TAG, "--MainActivity->>onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i(TAG, "--MainActivity->>onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i(TAG, "--MainActivity->>onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i(TAG, "--MainActivity->>onStop"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "--MainActivity->>onDestroy"); } }
MyFragment.java代码如下:
public class MyFragment extends Fragment { private final String TAG = "MyFragment"; //获得activity的传递的值 @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); Log.i(TAG, "--MyFragment->>onAttach"); } //实例化成员变量,可以在这个方法中初始化相关变量(如:连接数据库) @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Log.i(TAG, "--MyFragment->>onCreate"); } //给当前的fragment绘制UI布局,可以使用线程更新UI @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "--MyFragment->>onCreateView"); View view = inflater.inflate(R.layout.f1, null); // TODO Auto-generated method stub return view; } //表示activity执行oncreate方法完成了的时候会调用此方法 @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); Log.i(TAG, "--MyFragment->>onActivityCreated"); } //和activity一致 @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i(TAG, "--MyFragment->>onStart"); } //和activity一致 @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i(TAG, "--MyFragment->>onResume"); } //和activity一致 @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i(TAG, "--MyFragment->>onPause"); } //和activity一致 @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i(TAG, "--MyFragment->>onStop"); } //表示fragment销毁相关联的UI布局 @Override public void onDestroyView() { // TODO Auto-generated method stub super.onDestroyView(); Log.i(TAG, "--MyFragment->>onDestroyView"); } //销毁fragment对象 @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "--MyFragment->>onDestroy"); } //脱离activity @Override public void onDetach() { // TODO Auto-generated method stub super.onDetach(); Log.i(TAG, "--MyFragment->>onDetach"); } }
运行结果:
activity初次加载:
--MainActivity->>onCreate
--MyFragment->>onAttach
--MyFragment->>onCreate
--MyFragment->>onCreateView
--MyFragment->>onActivityCreated
--MainActivity->>onStart
--MyFragment->>onStart
--MainActivity->>onResume
--MyFragment->>onResume
电话打入
--MyFragment->>onPause
--MainActivity->>onPause
--MyFragment->>onStop
--MainActivity->>onStop
电话结束
--MainActivity->>onStart
--MyFragment->>onStart
--MainActivity->>onResume
--MyFragment->>onResume
销毁activity
--MyFragment->>onPause
--MainActivity->>onPause
--MyFragment->>onStop
--MainActivity->>onStop
--MyFragment->>onDestroyView
--MyFragment->>onDestroy
--MyFragment->>onDetach
--MainActivity->>onDestroy