getActivity()方法可以在fragment中获取activity的句柄进行下一步操作,然而getActivity取出的值有时会为空,这里面会产生一个时序问题,也就是activity还没有创建完,就在fragment中使用
public class TestFragment extends Fragment {
private static final String TAG = "Fragment";
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach");
}
}
- onAttach方法:Fragment和Activity建立关联的时候调用。
- onCreateView方法:为Fragment加载布局时调用。
- onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
- onDestroyView方法:Fragment中的布局被移除时调用。
- onDetach方法:Fragment和Activity解除关联的时候调用。
打印一下生命周期函数
从上图执行顺序看来,在fragment中的onCreateView 中执行后续操作是不保险的,因为activity的onCreate方法没有执行完,所有getActivity方法就有可能为null,
小小结:在fragment 的onCreateView中不做太多的操作,具体后续操作(initView, initData等均放到onActivityCreated中)
fragment和activity之间通信
-
Activity 传递参数到fragment中
//activity 中设置参数 TestFragment newFragment = new TestFragment (); Bundle args = new Bundle(); args.putInt(TestFragment .ARG_POSITION, position); newFragment.setArguments(args); //fragment 中获取参数 Bundle bundle = getArguments();
-
Fragment 传递参数到Activity中
// fragment 中定义一个接口 public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; public interface OnHeadlineSelectedListener { public void onArticleSelected(int position); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } } //触发事件是调用 if(mCallback != null) { mCallback.onArticleSelected(1); } //activity 中实现OnHeadlineSelectedListener 接口,就可以获得fragment传递过来的参数
backstack 问题
fragment add方发后没有调用addToBackStack, 和调用addToBackStack的数量问题
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
BKPaymentFragment paymentFragment = new BKPaymentFragment();
ft.add(getViewId("payment_content"), paymentFragment).commit();
//ft.add(getViewId("payment_content"), paymentFragment).addToBackStack(null).commit();
int num = getSupportFragmentManager().getBackStackEntryCount();
此时获得的num 数量均为0
@Override
public void onBackPressed() {
int num = getSupportFragmentManager().getBackStackEntryCount();
LogUtils.d("back fragment 数量" + num);
super.onBackPressed();
}
此时获得的num 数量 没有调用addToBackStack的为0, 调用过的为1