fragment 总结

2018-04-14  本文已影响0人  陆元伍

1、fragment生命周期

onAttach onCreate onCreateView onActivityCreated onStart onResume onPause onStop onDestroyView onDestroy onDetach

image

2、fragment与activity传值

activity传值给fragment:

Bundle bundle = new Bundle();
bundle.putString(Constant.INTENT_ID, productId);
Fragment fragment = new Fragment();
fragment.setArguments(bundel);

fragment传值给activity:

        //MainActivity实现MainFragment开放的接口                                
        public class MainActivity extends FragmentActivity implements FragmentListener{
        @override
        publicvoidtoH5Page(){ }
        }
        public class MainFragment extends Fragment{
            public FragmentListener mListener;
            //MainFragment开放的接口
            public static interface FragmentListener{
                //跳到h5页面
                void   toH5Page();
            }

          @Override
          public void onAttach(Activity activity) {
              super.onAttach(activity);
              //对传递进来的Activity进行接口转换
              if(activity instance FragmentListener){
                  mListener = ((FragmentListener)activity);
               }
          }
          ...其他处理
      }
上一篇下一篇

猜你喜欢

热点阅读