使用 Fragment&&Fragment的生命周期&&带侧边栏的 Activity&&Tabbed Activity

1、使用 Fragment

  • fragment作为界面切换的代价比Activity小。切换很方便。是可以理解为一个Activity的模版。
  • 跳转fragment:
    •  1 public View onCreateView(LayoutInflater inflater, ViewGroup container,
       2             Bundle savedInstanceState) {
       3         View rootView = inflater.inflate(R.layout.fragment_main, container, false);
       4         rootView.findViewById(R.id.ShowAnotherFragment).setOnClickListener(new View.OnClickListener() {    
       5             @Override
       6             public void onClick(View v) {
       7                 getFragmentManager().
       8                 beginTransaction().
       9                 addToBackStack(null).
      10                 replace(R.id.container, new anotherfragment()).
      11                 commit();
      12             }
      13         });
      14         return rootView;
      15     }
      View Code
    •  1 public View onCreateView(LayoutInflater inflater, ViewGroup container,
       2             Bundle savedInstanceState) {
       3         View rootview = inflater.inflate(R.layout.fragment_another, container,false);
       4         
       5         rootview.findViewById(R.id.Back).setOnClickListener(new View.OnClickListener() {
       6             @Override
       7             public void onClick(View v) {
       8                 getFragmentManager().popBackStack();
       9             }
      10         });
      11         
      12         return rootview;
      13     }
      View Code

2、Fragment的生命周期

  • Fragment的生命周 

              

  • 与Activity生命周期的对比

         

    场景演示 : 切换到该Fragment

    11-29 14:26:35.095: D/AppListFragment(7649): onAttach
    11-29 14:26:35.095: D/AppListFragment(7649): onCreate
    11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
    11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
    11-29 14:26:35.120: D/AppListFragment(7649): onStart
    11-29 14:26:35.120: D/AppListFragment(7649): onResume

    屏幕灭掉:

    11-29 14:27:35.185: D/AppListFragment(7649): onPause
    11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
    11-29 14:27:35.205: D/AppListFragment(7649): onStop

    屏幕解锁

    11-29 14:33:13.240: D/AppListFragment(7649): onStart
    11-29 14:33:13.275: D/AppListFragment(7649): onResume

    切换到其他Fragment:
    11-29 14:33:33.655: D/AppListFragment(7649): onPause
    11-29 14:33:33.655: D/AppListFragment(7649): onStop
    11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView

    切换回本身的Fragment:

    11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
    11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
    11-29 14:33:55.825: D/AppListFragment(7649): onStart
    11-29 14:33:55.825: D/AppListFragment(7649): onResume

    回到桌面

    11-29 14:34:26.590: D/AppListFragment(7649): onPause
    11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
    11-29 14:34:26.880: D/AppListFragment(7649): onStop

    回到应用

    11-29 14:36:51.940: D/AppListFragment(7649): onStart
    11-29 14:36:51.940: D/AppListFragment(7649): onResume

    退出应用

    11-29 14:37:03.020: D/AppListFragment(7649): onPause
    11-29 14:37:03.155: D/AppListFragment(7649): onStop
    11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
    11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
    11-29 14:37:03.165: D/AppListFragment(7649): onDetach

3、带侧边栏的Activity:

  • 这个接口是Activity与侧边栏之间的关键:
  • 1 public static interface NavigationDrawerCallbacks {
    2         /**
    3          * Called when an item in the navigation drawer is selected.
    4          */
    5         void onNavigationDrawerItemSelected(int position);
    6         
    7         void onGoLiyang();
    8     }
    View Code
  • 侧边栏获得Activity:
    •  1 @Override
       2     public void onAttach(Activity activity) {
       3         super.onAttach(activity);
       4         try {
       5             mCallbacks = (NavigationDrawerCallbacks) activity;
       6         } catch (ClassCastException e) {
       7             throw new ClassCastException(
       8                     "Activity must implement NavigationDrawerCallbacks.");
       9         }
      10     }
  • 侧边栏调用接口函数达到切换fragment目的:
    • public class SliderActivity extends Activity implements
              NavigationDrawerFragment.NavigationDrawerCallbacks {
      
          。。。。
      
          @Override
          public void onGoLiyang() {
              getFragmentManager()
              .beginTransaction()
              .replace(R.id.container, new WebLiyang())
              .commit();
          }
              。。。。
      
      }

4、Tabbed Activity使用

  • @Override
            public Fragment getItem(int position) {
                // getItem is called to instantiate the fragment for the given page.
                // Return a PlaceholderFragment (defined as a static inner class
                // below).
                //return PlaceholderFragment.newInstance(position + 1);
                switch (position) {
                case 0:
                    return new Image1Fm();
                case 1:
                    return new Image2Fm();
                case 2:
                    return new Image3Fm();
                }
                return null;
            }
posted @ 2015-05-27 13:55  何人之名  阅读(248)  评论(0)    收藏  举报