1.Fragment的生命周期:
简单在新建一个MyFragment继承自Fragment,重写各个生命周期回调方法,各个方法中直接输出标识相关函数被调用的信息。
重写MainActivity的各个生命周期回调方法,同样输出标识信息。
MyFragment.java:
public class MyFragment extends Fragment { @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); Log.i("FRAGMENT","onActivityCreated()"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub Log.i("FRAGMENT","onCreateView()"); TextView textView = new TextView(getActivity()); textView.setText("This is a fragment"); return textView; } @Override public void onDestroyView() { // TODO Auto-generated method stub super.onDestroyView(); Log.i("FRAGMENT","onDestroyView()"); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); Log.i("FRAGMENT","onAttach()"); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Log.i("FRAGMENT","onCreate()"); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i("FRAGMENT","onStart()"); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i("FRAGMENT","onResume()"); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i("FRAGMENT","onPause()"); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i("FRAGMENT","onStop()"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("FRAGMENT","onDestroy()"); } @Override public void onDetach() { // TODO Auto-generated method stub super.onDetach(); Log.i("FRAGMENT","onDetach()"); } }
注意:onCreateView()方法中需返回一个View对象。
MainActivity.java:
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("ACTIVITY","onCreate()"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i("ACTIVITY","onStop()"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("ACTIVITY","onDestroy()"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i("ACTIVITY","onPause()"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i("ACTIVITY","onResume()"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i("ACTIVITY","onStart()"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.i("ACTIVITY","onRestart()"); } }
布局文件:
<fragment android:id="@+id/frag" android:name="cn.csc.fragment.MyFragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
注意,添加的fragment需要通过name属性指定自己新建的fragment的权限定名。
运行结果:
按下模拟器的BACK按钮:
中间插入了dalvik的垃圾回收,不过整体的顺序是没有改变的:
整个生命周期各个回调函数的调用过程如下:
显示到前台:
MyFragment onAttach() 附加到activity上
MyFragment onCreate() fragment创建
MyFragment onCreateView() fragment创建自己的视图
MainActivity onCreate() activity创建
MyFragment onActivityCreated()
MainActivity onStart()
MyFragment onStart()
MainActivity onResume()
MyFragment onResume()
按后退键
MyFragment onPause()
MainActivity onPause()
MyFragment onStop()
MainActivity onStop()
MyFragment onDestoryView() 销毁掉自己的视图
MyFragment onDestory()
MyFragment onDetach() 解除和activity的关系
MainActivity onDetory()
2.一个简单的两个Fragmet联系的示例,点击其中一个另一个发生变化:
1)新建MyFragment继承自ListFragment:
public class MyFragment extends ListFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, android.R.id.text1, new String[]{"6:00","6:30","7:20","8:00","8:40"})); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); //设置为单选模式,单击选中 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); getListView().setItemChecked(position, true); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); Fragment fr = new TmpFragment(position); ft.replace(R.id.frame, fr); ft.commit(); } }
2)新建TmpFragment继承自Fragment:
public class TmpFragment extends Fragment { private int index; private static String[] items = new String[]{"起床洗漱","打拳散步","吃早饭","等公交","打卡"}; public TmpFragment(int index) { super(); this.index = index; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub TextView tv = new TextView(getActivity()); tv.setText(items[index]); return tv; } }
3)布局文件:
<fragment android:id="@+id/frag" android:name="cn.csc.fragment1.MyFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/frame" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> </FrameLayout>
4)运行结果:
以上就是Fragment的简单学习,需要时再认真学下……